Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Perl Modules Darby Tien-Hao Chang Department of Electrical Engineering, National Cheng Kung University."— Presentation transcript:

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

2 CPAN Comprehensive Perl Archive Network http://search.cpan.org/

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

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

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 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/Simple.pm  http://search.cpan.org/~gaas/libwww-perl- 5.805/lib/LWP.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 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 => 'http://host.com/index.html' );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 “ ”; print “ Test Template ”; print “ ”; print “My Home Directory is $ENV{HOME} ”; print “My Path is set to $ENV{PATH}”; print “ ”;

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

15 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


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

Similar presentations


Ads by Google