Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor:

Similar presentations


Presentation on theme: "Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor:"— Presentation transcript:

1 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor: Byrne Reese X401

2 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 2 Review from Last Week… Getting setup Your first program: helloworld Your first CGI: hello.cgi Homework: Web server setup

3 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 3 Today’s Agenda 1.Introduction to CPAN 2.Finish hello.cgi 3.Templating using Template Toolkit 4.Cookies! 5.Web Automation using LWP 6.Writing a Link Checker

4 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 4 CPAN

5 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 5 Using CPAN Comprehensive Perl Archive Network Installing Perl modules from the command line: –perl –MCPAN –e ‘shell’ –perl –MCPAN –e ‘install CGI’

6 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 6 Exercise Install the following modules: –CGI (‘install CGI’) –Template Toolkit (‘install Template’) –LWP (‘install LWP’) Note: accept the default configuration for all parameters, except: –Selecting your continent (5) –Selecting your country (4) –Selecting your mirrors (1,2,3,4,5)

7 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 7 Finishing hello.cgi

8 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 8 HTTP Overview HTTP Headers Cookies Query Parameters –Query Strings –POST Parameters –Files Writing Your Own CGI module

9 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 9 Don’t Re-invent the Wheel: Use CGI.pm

10 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 10 CGI.pm “use”-ing the module –use CGI qw/:standard/; –use CGI; $query = new CGI; The param() subroutine The header() subroutine –Set the content-type –Set cookies Testing via the command line

11 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 11 Hello World #!/usr/bin/perl use CGI qw/:standard/; print header; if (param(‘say_hello_to’)) { print “Hello “. param(‘say_hello_to’); } else { print <<END_HTML;... END_HTML }

12 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 12 Enhancing hello.cgi Why use templates? –Separate form and function, or “app logic and presentation layer” –Cleaner code –Easier to edit HTML –Allows non-technical and technical people to collaborate more easily Using Perl’s Template Toolkit

13 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 13 A Templated Hello World Example: hello.tt: [% PAGETITLE %] Enter name: <input type="text" name="say_hello_to" size="10" value=“[% SAYHELLOTO %]” /> http://www.majordojo.com/ucex/interperl/hello.tt

14 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 14 A Templated Hello World Example: hello.cgi: use CGI; use Template; my $query = new CGI; my $tt = Template->new({ INCLUDE_PATH => "./tmpl", }); print $query->header(-type => 'text/html'); $tt->process('hello.tt', { sayhelloto => $query->param('foo'), pagetitle => 'Hello World!', } ) || die $tt->error;

15 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 15 Enhancing hello.cgi Using HTTP cookies –Stateful applications –Single Sign-On Exercise (if there is time)

16 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 16 Writing Cookies using CGI # prelude $cookie = $query->cookie( -name =>'mycookie', -value =>'xyzzy'); print $query->header(-cookie => $cookie); # yadda yadda yadda

17 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 17 Reading Cookies using CGI # prelude my $cookie = cookie('mycookie'); # yadda yadda yadda

18 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 18 Exercise Add a “Remember Me” checkbox to the Hello World form: –If the user has a cookie set that identifies them as a returning user, then do not prompt them for a name to say hello to. –The cookie should only be “session based.” Extra Credit: –Give the user a way of clearing the cookie without restarting the browser.


Download ppt "Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 1 Intermediate Perl Programming Class Two Instructor:"

Similar presentations


Ads by Google