Eleven CPAN Modules

Chris Dolan

Equilibrious LLC


May 9, 2007

What is CPAN?

Several things:

What is CPAN?

The most valuable part of Perl!

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!)


Module #: CPAN.pm

$ 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:

Module #: File::Slurp

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;

Module #: Path::Class

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;

Module #: LWP::Simple

LWP is short for "lib-www-perl"

use LWP::Simple;
getstore('http://cpan.org', 'index.html');

Print the top ten Google results for "cpan":

print "$_\n" for
       =~ /href="([^"]+)" class=l/g;

Module #: WWW::Mechanize

use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->field('q', 'cpan');
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

Module #: File::Temp

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.

Module #: File::HomeDir

use File::HomeDir;
use Path::Class;
my $homedir = File::HomeDir->my_home;
mkdir dir($homedir, '.cache');

Module #: Test::More

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
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.

Module #: DBI

The universal Database Interface
use DBI;
my $dbh = DBI->connect('dbi:mysql:database=store',
                       'admin', 's3cre3t');
my $sth = $dbh->prepare('select * from products');
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.

Module #: DateTime

use DateTime;
my $dt = DateTime->now(time_zone =>
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.

Module #: Pod::POM::Web

use Pod::POM::Web;
system 'open', 'http://localhost:8080';

A standalone, web-browsable directory of all Perl modules installed on your computer, including both documentation and source.


Finding more modules

Sometimes good modules are easy to find (like Net::FTP). Others are harder.