Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University
CPAN Comprehensive Perl Archive Network
CGI.pm Simple Common Gateway Interface class You can find some resources from CPAN: Source Typical usage Function-by-function description How do I know the module name “CGI”?
LWP::Simple Simple procedural interface to LWP So, what is LWP? The World-Wide Web library for Perl get($url) How do we get a POST web page? head($url) getprint($url) getstore($url, $file) mirror($url, $file) is_success($rc) is_error($rc)
How to POST? #!/usr/bin/perl -w # dependent modules use strict; use LWP::UserAgent; my $url = " = { "pdbcode" => "1bck" }; my $ua = new LWP::UserAgent; my $response = $ua->post( $url, ); if ( $response->is_success ) { print $response->content; # or whatever } else { die $response->status_line; }
Compare to the GET example #!/usr/bin/perl -w # dependent modules use strict; use LWP::Simple; my $url = " my $web = &get( $url ); print "$web“;
What is LWP::UserAgent? It’s a browser! It contain almost everything of your browser… if Perl can mimic LWP Library version number and documentation LWP::MediaTypes MIME types configuration (text/html etc.) LWP::Debug Debug logging module LWP::Simple Simplified procedural interface for common functions HTTP status code (200 OK etc) Date parsing module for HTTP date formats HTTP content negotiation calculation File::Listing Parse directory listings HTML::Form Processing for s in HTML documents
LWP, LWP::Simple and LWP::UserAgent, what a mess! You can include LWP directly use LWP; There are some clues in the CPAN /lib/LWP/Simple.pm /lib/LWP/Simple.pm /lib/LWP.pm /lib/LWP.pm
Multi-steps extraction #!/usr/bin/perl -w use LWP::Simple; my $term = “ 棒球 "; my $url = " my $web = &get( $url ); $web =~ /href=([^>"]+)>$term/ or die; $url = " $web = &get( $url );
A more browser-like Perl module WWW::Mechanize use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get( $url ); $mech->follow_link( n => 3 ); $mech->follow_link( text_regex => qr/download this/i ); $mech->follow_link( url => ' ); $mech->submit_form( form_number => 3, fields => { username => 'mungo', password => 'lost-and-alone', } ); $mech->submit_form( form_name => 'search', fields => { query => 'pot of gold', }, button => 'Search Now‘ );
Consider a CGI program to show some server information #!/usr/bin/perl -w print “Content-type: text/html\n\n”; print “ ”; print “ Test Template ”; print “ ”; print “My Home Directory is $ENV{HOME} ”; print “My Path is set to $ENV{PATH}”; print “ ”;
Now suppose that we have a wizard-like web application Step1.html Input your first name, last name and Step2.pl Validate the and one more question Step3.pl Show the final page
How will you design the three page? They are supposed to be very similar And there could be numerous redundant code
One more Perl module to help CGI.pm HTML::Template Test Template My Home Directory is My Path is set to #!/usr/bin/perl -w use HTML::Template; # open the html template my $template = HTML::Template->new(filename => 'test.html'); # fill in some parameters $template->param(HOME => $ENV{HOME}); $template->param(PATH => $ENV{PATH}); # send the obligatory Content-Type and print the template output print "Content-Type: text/html\n\n", $template->output;
The tags TMPL_VAR TMPL_LOOP Name: Job: $template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'programmer' }, { name => 'Steve', job => 'soda jerk' }, ] ); print $template->output(); TMPL_IF, TMPL_ELSE