Perl itself is nice, but it's not really that different from Python, Ruby, or even PHP and VB
What makes Perl powerful is the HUGE collection of well-tested, open-source code that extends it
Over 11,000 packages from almost 6,000 authors (probably 75% crap, but that still leaves a lot of non-crap!)
$ cpan cpan[1]> install File::Slurp ... cpan[2]> exit
Download, unpack, build, test, install
The very first time, this will ask you about 40 questions:
ftp://mirror.sit.wisc.edu/pub/CPAN/ ftp://cpan.mirrors.tds.net/pub/CPAN/ ftp://mirrors.kernel.org/pub/CPAN/
use File::Slurp; my @files = read_dir('lib'); for my $file (@files) { my $content = read_file($file); if ($content =~ /CPAN/) { append_file('cpan.txt', $file, "\n"); } }
Absurdly simple, but easier than:
open my $fh, '<', $file or die; my $content = do {local $/ = undef; <$fh>}; close $fh or die;
use Path::Class; my $dir = dir('foo', 'bar'); # foo/bar my $subdir = $dir->subdir('baz'); # foo/bar/baz my $file = $dir->file('file.txt'); # foo/bar/file.txt my $fh = $file->openr(); print <$fh>; close $fh; $file->remove();
LWP is short for "lib-www-perl"
use LWP::Simple; getstore('http://cpan.org', 'index.html');
Print the top ten Google results for "cpan":
$LWP::Simple::ua->agent('Firefox'); print "$_\n" for get('http://google.com/search?q=cpan') =~ /href="([^"]+)" class=l/g;
use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->agent('Firefox'); $mech->get('http://www.google.com/'); $mech->form_number(1); $mech->field('q', 'cpan'); $mech->submit(); for my $link ($mech->links) { next if !$link->attrs->{class}; next if $link->attrs->{class} ne 'l'; print $link->url, "\n"; }Same result as the LWP::Simple example
use File::Temp; use Path::Class; my $tempdir = tempdir(CLEANUP => 1); my $fh = file($tempdir, 'foo.txt')->openw;The
CLEANUP
option auto-deletes the directory at program termination.
use File::HomeDir; use Path::Class; my $homedir = File::HomeDir->my_home; mkdir dir($homedir, '.cache');
$ENV{HOME}
or (getpwuid $>)[7]
(shudder...)use Test::More tests => 5; ok(2+2 == 4, 'check 2+2'); is(2+2, 4, 'check 2+2'); cmp_ok(2+2, '>', 3, 'lower bound'); isnt(2+2, 5, 'nope, not 5'); is(2/2, 2, 'check division');
$ perl test.pl 1..5 ok 1 - check 2+2 ok 2 - check 2+2 ok 3 - lower bound ok 4 - nope, not 5 not ok 5 - check division # Failed test 'check division' # at test2.pl line 6. # got: '1' # expected: '2' # Looks like you failed 1 test of 5.
use DBI; my $dbh = DBI->connect('dbi:mysql:database=store', 'admin', 's3cre3t'); my $sth = $dbh->prepare('select * from products'); $sth->execute; while (my $row = $sth->fetchrow_hashref) { print "$row->{id}: $row->{title}\n"; }For a much higher-level interface, try DBIx::Class. However the learning curve is greater.
use DateTime; my $dt = DateTime->now(time_zone => 'America/Chicago'); print $dt->hour, ':', $dt->minute, "\n"; $dt->add( months => 3 ); my $seconds_since_1970 = $dt->epoch; utime $time, $time, 'file.txt';DateTime is a VERY thorough implementation of dates, times, timezones and durations.
use Pod::POM::Web; Pod::POM::Web->server; system 'open', 'http://localhost:8080';
A standalone, web-browsable directory of all Perl modules installed on your computer, including both documentation and source.
Demo...
Sometimes good modules are easy to find (like Net::FTP). Others are harder.