Table of Contents

An interactive perl shell

f perlsh.pl

#!perl

use strict;
no strict 'vars';
no strict 'refs';
use warnings;

use Data::Dumper;
$Data::Dumper::Indent--;

$| = 1;

# an alias for exit()
sub quit { exit; }

my ($ver,$maj,$min) = ($] =~ /(\d+)\.(\d{3})(\d{3})/);
$maj += 0;
$min += 0;
print +(split '/', $^X)[-1], " $ver.$maj.$min\n";

$, = ',';
$THE_PROMPT = '% ';
print $THE_PROMPT;
while (<>) { print eval; print +($@ || "\n") . $THE_PROMPT };

usage:

$ alias perlsh='perl ~/bin/perlsh.pl'
$ perlsh
perl.exe 5.10.0
% @l=qw(the quick brown fox jumps over the lazy dog)
the,quick,brown,fox,jumps,over,the,lazy,dog
% @sorted_by_length = map {$_->[0]} sort {$a->[1] <=> $b->[1]} map {[$_, length]} @l
the,fox,the,dog,over,lazy,quick,brown,jumps

Optimizing Perl

First run the script with profiling enabled:

 perl -d:DProf myscript.pl

then run the profile-dumper:

 dprofpp tmon.out

Copy (duplicate) a two-dimensional array in Perl

 @target = map [map $_ = $_, @{$_}], @source;

CPAN modules fail to compile

Then you're probably on a RedHat >= v9 system. Do this:

export LANG=C

and retry.

Get a Perl-module from CPAN

The easy way which also automatically fetches all modules needed by the module:

perl -MCPAN -e 'install modulename'

e.g.:

perl -MCPAN -e 'install WWW::Search'