Introduction to Perl Programming

Download Report

Transcript Introduction to Perl Programming

Intermediate Perl Programming

Class Two

Instructor: Byrne Reese X401 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

1

Review from Last Week…

• Getting setup • Your first program: helloworld • Your first CGI: hello.cgi

• Homework: Web server setup Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

2

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 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

3

CPAN

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

4

Using CPAN

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

5

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) Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

6

Finishing hello.cgi

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

7

HTTP Overview

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

8

Don’t Re-invent the Wheel: Use CGI.pm

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

9

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 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

10

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 } Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

11

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 Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

12

A Templated Hello World

Example: hello.tt: [% PAGETITLE %]

Enter name: value=“[% SAYHELLOTO %]” /> http://www.majordojo.com/ucex/interperl/hello.tt

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

13

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; Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

14

Enhancing hello.cgi

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

15

Writing Cookies using CGI

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

16

Reading Cookies using CGI

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

17

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.

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

18