1 / 15

Perl Modules

Perl Modules. Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University. CPAN Comprehensive Perl Archive Network. http://search.cpan.org/. CGI.pm Simple Common Gateway Interface class. You can find some resources from CPAN: Source Typical usage

artie
Download Presentation

Perl Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University

  2. CPANComprehensive Perl Archive Network • http://search.cpan.org/

  3. 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”?

  4. 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)

  5. 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; • }

  6. 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“;

  7. 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 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

  8. 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

  9. 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 );

  10. 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‘ • );

  11. 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>”;

  12. 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

  13. How will you design the three page? • They are supposed to be very similar • And there could be numerous redundant code

  14. 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;

  15. 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

More Related