<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="presenter.xsl"?>
<slideshow>
	<title>Perl Modules</title>
	<slide>
		<title>Perl Modules</title>
		<body>
			<p>By Karl Voelker</p>
		</body>
	</slide>
	<slide><title>Perl Modules</title><body>
		<p>Perl is well-known for its vast collection of modules 
			called CPAN at 
			<a href='http://www.cpan.org/'>
				http://www.cpan.org/</a>.</p>
		<p>You can install modules from CPAN by running 
			<tt>cpan</tt> as root on a UNIX-like system with 
			Perl.</p>
	</body></slide>
	<slide><title>Module Concepts</title><body>
		<p>You include a module with <tt>use</tt>.</p>
		<p>A <tt>use</tt> statement may include a list of 
			module subroutines to import.</p>
	</body></slide>
	<slide><title>List of Modules</title><body>
		<p>We can't cover every Perl module, so we'll just 
			cover the ones I find most useful:</p>
		<div style='font-size: 0.85em'>
		<ul class='col'>
		<li>Carp</li>
		<li>CGI</li>
		<li>Data::Dumper</li>
		<li>Date::Calc</li>
		<li>Device::ParallelPort</li>
		<li>Digest::SHA</li>
		<li>DBI</li>
		<li>Exporter</li>
		<li>File::Temp</li>
		</ul>
		<ul class='col'>
		<li>Getopt::Std</li>
		<li>IO::Socket::INET</li>
		<li>IPC::Open2</li>
		<li>List::Util</li>
		<li>LWP::UserAgent</li>
		<li>Mail::Sendmail</li>
		<li>Net::Server</li>
		<li>Time::HiRes</li>
		</ul>
		</div>
	</body></slide>
	<slide><title>Carp</title><body>
		<p>Use <tt>Carp</tt> to:</p>
		<dl>
		<dt>Obtain a printable stack trace:</dt>
		<dd><tt>Carp::longmess</tt></dd>
		<dt>When writing a module, deal with errors that 
			aren't your fault:</dt>
		<dd><ul>
			<li><tt>Carp::carp</tt> (like <tt>warn</tt>)</li>
			<li><tt>Carp::croak</tt> (like <tt>die</tt>)</li>
		</ul></dd>
		</dl>
	</body></slide>
	<slide><title>CGI</title><body>
		<p>The CGI module is for writing web applications
			with the Common Gateway Interface.</p>
		<pre>use CGI qw/header param/;

print header(-type => 'text/plain');
print "Hello, ", param('name'), "!\n";</pre>
	</body></slide>
	<slide><title>Data::Dumper</title><body>
		<p><tt>Data::Dumper</tt> prints data structures as
			valid Perl:</p>
		<pre>use Data::Dumper;

print Dumper(
	{'foo' => [1, 2, 3], 
	 'bar' => {'baz' => 4, 
		   'quux' => 5}});</pre>
	</body></slide>

	<slide><title>Date::Calc</title><body>
		<p><tt>Date::Calc</tt> makes math with dates easy.</p>
		<pre>use Date::Calc qw/Delta_Days Today/;

my $delta = Delta_Days(
		Today, 2008, 12, 25);
print "$delta days until Christmas!\n";</pre>
	</body></slide>

	<slide><title>Device::ParallelPort</title><body>
		<p><tt>Device::ParallelPort</tt> makes programming for a 
			parallel port fun (on Linux or Windows)!</p>
		<pre>use Device::ParallelPort::drv::linux;

my $port = Device::ParallelPort->new;
foreach my $bit (0..7) {
	$port->set_bit($bit, 1);
	sleep 1;
	$port->set_bit($bit, 0);
}</pre>
	</body></slide>

	<slide><title>Digest::SHA</title><body>
		<p><tt>Digest::SHA</tt> calculates SHA hashes with ease!</p>
		<pre>use Digest::SHA qw/sha512_hex/;

print sha512_hex("secret password");</pre>
	</body></slide>

	<slide><title>DBI</title><body>
		<p><tt>DBI</tt> provides a standard interface to 
			any relational database.</p>
		<pre>use DBI;

my $dbh = DBI->connect("DBI:mysql:$db", 
	$user, $pass);
my $sth = $dbh->prepare('SELECT * 
	FROM cheese WHERE age > ?');
$sth->execute(10);
my $res = $sth->fetchall_arrayref({});</pre>
	</body></slide>

	<slide><title>Exporter</title><body>
		<p><tt>Exporter</tt> lets a module make functions 
			available for other modules to import.</p>
		<pre>package Foo;
use base qw/Exporter/;
our @EXPORT_OK = qw/bar/;
sub bar { print "Hello, World!\n"; }</pre>
		<hr/>
		<pre>package Baz;
use Foo qw/bar/;
bar();</pre>
	</body></slide>

	<slide><title>File::Temp</title><body>
		<p><tt>File::Temp</tt> can safely open a temporary 
			file.</p>
		<pre>use File::Temp qw/tempfile/;

my ($fh, $filename) = tempfile;
print $fh "This file is $filename.\n";

close $fh;</pre>
	</body></slide>

	<slide><title>Getopt::Std</title><body>
		<p><tt>Getopt::Std</tt> parses command-line arguments 
			for switches.</p>
		<pre>use Getopt::Std qw/getopts/;

getopts('hv:', \%opt);
if ($opt{'h'}) {
    print "You asked for help.\n";
} else {
    print "Verbosity: $opt{'v'}.\n";
}</pre>
	</body></slide>

	<slide><title>IO::Socket::INET</title><body>
		<p><tt>IO::Socket::INET</tt> makes network 
			communication easy.</p>
		<pre>use IO::Socket::INET;

my $sock = IO::Socket::INET->new(
	'PeerHost' => 'foo.net', 
	'PeerPort' => 1111, 
	'Proto' => 'tcp');
print $sock "Foo\n";
print &lt;$sock&gt;;
$sock->close;</pre>
	</body></slide>

	<slide><title>IPC::Open2</title><body>
		<p><tt>IPC::Open2</tt> runs a process and gives you 
			two-way communication with it.</p>
		<pre>use IPC::Open2;

open2($out, $in, 'bc');
print $out "3 * 4\n";
my $twelve = &lt;$in&gt;;
print $out "quit\n";
print $twelve;</pre>
	</body></slide>

	<slide><title>List::Util</title><body>
		<p><tt>List::Util</tt> provides utility functions 
			for lists.</p>
		<pre>use List::Util qw/reduce shuffle/;

my @list = (1, 2, 3, 4);
my $sum = reduce { $a + $b } @list;

my @cards = shuffle 0..51;</pre>
	</body></slide>

	<slide><title>LWP::UserAgent</title><body>
		<p><tt>LWP::UserAgent</tt> enables browsing the web, 
			even sites that use cookies.</p>
		<pre>use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = 
    $ua->get('http://www.google.com/');
if ($response->is_success) {
    print $response->content;
} else {
    die $response->status_line;
}</pre>
	</body></slide>

	<slide><title>Mail::Sendmail</title><body>
		<p><tt>Mail::Sendmail</tt> lets you send electronic mail.</p>
		<pre>use Mail::Sendmail;

sendmail(
    'To'      => 'me@karlv.net', 
    'From'    => 'perl@karlv.net', 
    'Subject' => 'Testing', 
    'Message' => "Hello, World!\n");</pre>
	</body></slide>

	<slide><title>Net::Server</title><body>
		<p><tt>Net::Server</tt> is a family of modules that provide 
			simple, forking, and multiplexing server daemons.</p>
		<pre>package Echo::Server;
use base qw/Net::Server/;

sub process_request {
    print while &lt;&gt;;
}

__PACKAGE__->run('port' => 4242);</pre>
	</body></slide>

	<slide><title>Time::HiRes</title><body>
		<p><tt>Time::HiRes</tt> lets you measure time with 
			microsecond resolution.</p>
		<pre>use Time::HiRes /gettimeofday usleep/;

my ($sec, $usec) = gettimeofday;

usleep 1000000;</pre>
	</body></slide>

	<slide><title>The End</title><body>
		<p>Questions?</p>
	</body></slide>

</slideshow>
