![Page 1: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/1.jpg)
Perl Modules
Darby Tien-Hao Chang
Department of Electrical Engineering, National Cheng Kung University
![Page 2: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/2.jpg)
CPANComprehensive Perl Archive Network http://search.cpan.org/
![Page 3: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/3.jpg)
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”?
![Page 4: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/4.jpg)
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)
![Page 5: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/5.jpg)
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; }
![Page 6: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/6.jpg)
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“;
![Page 7: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/7.jpg)
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
![Page 8: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/8.jpg)
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
![Page 9: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/9.jpg)
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 );
![Page 10: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/10.jpg)
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‘
);
![Page 11: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/11.jpg)
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>”;
![Page 12: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/12.jpg)
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
![Page 13: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/13.jpg)
How will you design the three page? They are supposed to be very similar And there could be numerous redundant
code
![Page 14: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/14.jpg)
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;
![Page 15: Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f455503460f94c670a6/html5/thumbnails/15.jpg)
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