Ten Modules I Wouldn't Go

Anywhere Without

Simon Cozens

NetThink Open Source Consultancy

Why?

  • "Repetitive Programming"

  • But that's OK, we have CPAN!...

  • CPAN is BIG.

  • How do I know it's any good?

  • Personal recommendations

(page 2)

----

Here's my personal Top 10...

  • 10. Bundle::CPAN

  • 9. Bundle::LWP

  • 8. Mail::Send

  • 7. MLDBM

  • 6. Date::Calc

  • 5. DBI

  • 4. Data::Dumper

  • 3. POE

  • 2. File::Spec

  • 1. XML::Simple

(page 3)

----

Bundle::CPAN

  • Cheating, really...

  • File::Spec is in core (and is top module #2!)

  • MD5 for validating downloads

  • Compress::Zlib

  • Archive::Tar

  • Bundle::libnet

  • Term::ReadLine

  • Term::ReadKey

  • And, of course...

  • CPAN.pm!

(page 4)

----

Bundle::CPAN

MD5

  • Simple, really

use Digest::MD5 qw(md5_hex);

print md5_hex($data);

(page 5)

----

Bundle::CPAN

Compress::Zlib

  • Allows both in-memory compression and file access

  • File access is much more useful

use Compress::Zlib;

$fh = gzopen($filename, $mode);

$bytesread = $fh->gzreadline($line);

$byteswritten = $fh->gzwrite($buffer);

  • (PerlIO in 5.8.0 should make this transparent anyway)

(page 6)

----

Bundle::CPAN

Archive::Tar

  • Portable .tar(.gz) file creation and reading

use Archive::Tar;

Archive::Tar->create_archive ("my.tar.gz", 9,

"/this/file", "/that/file");

$tar = Archive::Tar->new();

$tar->add_files("file/foo.c", "file/bar.c");

$tar->write("files.tar");

(page 7)

----

Bundle::CPAN

Bundle::libnet

  • Now in core!

Net::FTP RFC959 File Transfer Protocol

Net::SMTP RFC821 Simple Mail Transfer Protocol

Net::Time RFC867 Daytime Protocol

Net::Time RFC868 Time Protocol

Net::NNTP RFC977 Network News Transfer Protocol

Net::POP3 RFC1939 Post Office Protocol 3

Net::SNPP RFC1861 Simple Network Pager Protocol

  • Should be regarded as building blocks.

(page 8)

----

Bundle::CPAN

Term::Read*

  • Allow history, editing and navigation

  • Allows turning off echo, etc.

use Term::ReadLine;

$term = new Term::ReadLine 'Simple Perl calc';

$prompt = "Enter your arithmetic expression: ";

$OUT = $term->OUT || STDOUT;

while ( defined ($_ = $term->readline($prompt)) ) {

$res = eval($_), "\n";

warn $@ if $@;

print $OUT $res, "\n" unless $@;

$term->addhistory($_) if /\S/;

}

(page 9)

----

Bundle::CPAN

CPAN

  • Makes downloading and installing modules trivial

  • Though you probably still want PPM on Win32

riot-act:/home/simon# perl -MCPAN -e shell

cpan shell -- CPAN exploration and modules installation (v1.59_54)

ReadLine support available (try 'install Bundle::CPAN')

cpan> install Some::Module

perl -MCPAN -e 'install Some::Module'

(page 10)

----

Bundle::LWP

  • It's really LWP::Simple

$page = get("http://www.netthink.co.uk");

  • getprint($url);

getstore($url, $file);

head($url)

mirror($url, $file);

(page 11)

----

Mail::Send

  • Abstraction layer for mail sending

use Mail::Send;

$msg = new Mail::Send

Subject=> "Some subject",

To => "simon@netthink.co.uk";

$msg->add("X-Mailer:", "Mail::Send");

$fh = $msg->open;

print $fh "Hello!\n";

$fh->close; # Sent!

(page 12)

----

MLDBM

  • Allows multi-level DBMs

  • Best way to dump data structures to disk

use MLDBM qw(DB_File);

tie %hash, "MLDBM", "persistent" or die $!;

$hash{"foo"} = [1, 2, 3, 4];

(page 13)

----

Date::Calc

  • ALL your date manipulation needs!

  • Delta_Days for calculating days between X and Y

  • Decode_Date_* for parsing a user-specified date

  • Date_to_Text(_Long) for printing a date appropriately

  • Multilingual support

  • Lots, *lots* more.

(page 14)

----

DBI

  • Abstracted RDBMS access

  • DBD:: drivers for pretty much everything

use DBI; use DBD::Mysql;

my $dbh = DBI->connect(":dbi:mysql:somedatabase", $user, $pw) || ...;

my $sth = $dbh->prepare("SELECT * FROM foo WHERE name IS NOT NULL");

$sth->execute;

my $matches = $sth->rows();

print "$matches matches found\n";

print "@row\n" while @row = $sth->fetchrow_array;

(page 15)

----

Data::Dumper

  • Core module

  • REALLY useful for debugging

use Data::Dumper;

print "State of hash:\n", Dumper($href);

(page 16)

----

POE

  • Award winning module!

"POE is an application kernel that uses event driven state machines as

threads. It includes a high-level I/O library that hides most of the

usual client/server tediosity."

  • It's a thing for making servers and clients.

  • It lets you do threads without threads.

  • It's growing into a small operating system.

(page 17)

----

File::Spec

  • Core module

  • Portable file handling

  • WHY OH WHY DON'T MORE PEOPLE USE THIS?

use File::Spec::Functions;

sub which {

my $program = shift;

for (path()) {

my $test = catfile($_, $program);

return $test if -e $test and -x $test;

}

}

(page 18)

----

XML::Simple

  • Sort of like Data::Dumper

  • But much, much cooler

use XML::Simple;

$hashref = XMLin($filename);

print FILE XMLout($filename);

(page 19)

----

And more...

Parse::RecDescent

Mail::Internet

Mail::Audit

File::Temp (core)

...

(page 20)