perl modules darby tien-hao chang department of electrical engineering, national cheng kung...
TRANSCRIPT
Perl Modules
Darby Tien-Hao Chang
Department of Electrical Engineering, National Cheng Kung University
CPANComprehensive Perl Archive Network http://search.cpan.org/
CGI.pmSimple 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::SimpleSimple 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 = "http://www.ebi.ac.uk/thornton-srv/databases/cgi-bin/pdbsum/GetPage.pl"; my @form = { "pdbcode" => "1bck" };
my $ua = new LWP::UserAgent; my $response = $ua->post( $url, \@form );
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 = "http://www.pdb.org/pdb/navbarsearch.do?inputQuickSearch=1bck";
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 function
s HTTP::Status HTTP status code (200 OK etc) HTTP::Date Date parsing module for HTTP date formats HTTP::Negotiate HTTP content negotiation calculation File::Listing Parse directory listings HTML::Form Processing for <form>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
http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/Simple.pm
http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP.pm
Multi-steps extraction
#!/usr/bin/perl -w
use LWP::Simple;
my $term = “棒球 "; my $url = "http://tw.sports.yahoo.com/bj2008/schedule.html"; my $web = &get( $url );
$web =~ /href=([^>"]+)>$term/ or die;
$url = "http://tw.sports.yahoo.com/bj2008/$1";
$web = &get( $url );
A more browser-like Perl moduleWWW::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 => 'http://host.com/index.html' ); $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 “<html>”; print “<head>”; print “<title>Test Template</title>”; print “</head>”; print “<body>”; print “My Home Directory is $ENV{HOME}<br />”; print “My Path is set to $ENV{PATH}”; print “</body>”; print “</html>”;
Now suppose that we have a wizard-like web application Step1.html
Input your first name, last name and e-mail Step2.pl
Validate the e-mail 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.pmHTML::Template <html>
<head> <title>Test Template</title>
</head> <body>
My Home Directory is <TMPL_VAR NAME=HOME><br /> My Path is set to <TMPL_VAR NAME=PATH>
</body> </html>
#!/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_VAR NAME="PARAMETER_NAME">
TMPL_LOOP <TMPL_LOOP NAME=EMPLOYEE_INFO>
Name: <TMPL_VAR NAME=NAME><br/> Job: <TMPL_VAR NAME=JOB>
</TMPL_LOOP>
$template->param(EMPLOYEE_INFO => [ { name => 'Sam', job => 'programmer' }, { name => 'Steve', job => 'soda jerk' },
] ); print $template->output();
TMPL_IF, TMPL_ELSE