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