Upload
gyles-harrell
View
227
Download
0
Tags:
Embed Size (px)
Citation preview
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI Programming &
The CGI.pm Perl Module
Instructor: Joseph DiVerdi, Ph.D., MBA
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI in a Nutshell
• CGI specifies how a Web Server software to communicates with other programs running on the server computer
• Why Bother?– Create Dynamic Web Pages
• "on-the-fly"
– Process user-supplied HTML form data
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI & Perl
• CGI applications can be written in almost any language– But CGI & Perl have become virtually synonymous
• "Perl is the duct tape of the Internet"– Hassan Schroeder, Sun's First Webmaster
• Perl is by far the most widely used language– For creating CGI Programs
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI & Perl
• Easy to Learn– Resembles Other Popular Languages– Forgiving With Detailed Error Messages
• Rapid Development Cycle– Interpreted Without a Separate Compile Phase
• Easily Portable– Available on Many Platforms
• Extremely Powerful String Manipulation– Powerful Built-In String Operators– Native Regular Expression Support
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Setting Up For CGI Programs
• Create cgi Directory in html Directory– Case Is Very Important!
• Ensure cgi Directory Has 755 Permission– HTML-Kit Does This Automatically
• Programs Must Be Placed in This Directory– Server Is Configured Only to Execute From There– Programs Viewed From Other Directories
• Will Not Execute • Program Contents Will Be Rendered on the Browser
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Short Examples
• Following Series of Code Snippets– Short Examples – Demonstrate Several Ways– Emit HTML From a Perl CGI Program
• "There's More Than One Way to Do It"– Programming Perl
• Worth Studying– You Can Develop Your Own Preferred Style– Some Methods Are Better in Some Situations– You Can Read Other Programmers' Code
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Simple CGI Program in Perl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>\nHello world!\n</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "Hello world!\n";
print "</BODY>\n";
print "</HTML>\n";
exit;
• Create a File Named "simple.pl"
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Simple CGI Program in Perl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>\nHello world!\n</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print scalar(localtime(time())), "\n";
print "</BODY>\n";
print "</HTML>\n";
exit;
• Create a File Named "time.pl"
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Escaping Quotes
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD>";
print "<TITLE>Hello world!</TITLE>";
print "</HEAD>";
print "<BODY BGCOLOR=\"#66FFFF\">";
print scalar(localtime(time())), "\n";
print "</BODY>";
print "</HTML>";
exit;
• Modify time.pl• Escaping Will Get Old Fast
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Pick Your Own Quotes
#!/usr/bin/perl
print qq(Content-type: text/html\n\n);
print qq(<HTML>\n);
print qq(<HEAD>\n);
print qq(<TITLE>\nHello world!\n</TITLE>\n);
print qq(</HEAD>\n);
print qq(<BODY BGCOLOR="#66FFFF">\n);
print scalar(localtime(time())), "\n";
qq(Hello world!\n);
print qq(</BODY>\n);
print qq(</HTML>\n);
exit;
• Called "Double Quote" Operator
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
"Here" Documents
#!/usr/bin/perl
print <<__END_OF_HTML__;
Content-type: text/html
<HTML>
<HEAD>
<TITLE>hello world!</TITLE>
</HEAD>
<BODY>Hello world!</BODY>
</HTML>
__END_OF_HTML__
exit;
• Be Careful of Ending Delimiter
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Multiple Delimiters
#!/usr/bin/perl
print <<__HTML_1__;
Content-type: text/html
<HTML><HEAD><TITLE>hello world!</TITLE></HEAD>
<BODY>
__HTML_1__
print scalar(localtime(time())), "\n";
print <<__HTML_2__;
</BODY></HTML>
__HTML_2__
exit;
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Perl Modules
• There's Got to Be a Better Way...• Modular System of Reusable Code
– Created by Many Different Authors– Archived in Massive CPAN System– Easily Imported Into Your Programs– Provides Functionality at Many Levels
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI.pm Module
• Become the Standard Tool in Perl– For Creating CGI Scripts
• Provides Clean & Simple Interface– For Emitting HTML Code From Scripts– For Most Common CGI Tasks
• Generates HTML Output• Accepts FORM Input
– Can Parse FORM Input Parameters
• Provides Error Handling
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Handling Input
• Environment Information– Methods to Access %ENV Information
• FORM Input– Automatically Parses FORM Data– Provides Simple Methods for Access
• File Upload– Permits CGI Script to Handle HTTP File Uploads
• Easily, Safely, & Transparently
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Generating Output
• Generating Headers– Methods to Emit HTTP Headers
• Various Content & Server Redirection
• Generate HTML– Method to Generate Full HTML Documents
• HTML Generation Alternatives– Quoted HTML & Here Documents
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Error Handling
• Trapping Die– die Does Not Work Well With CGI
• "Internal Server Error" Report
• CGI::Carp– Provides Methods for Trapping Die & Other Errors
• Custom Solutions– Provides More Control When Displaying Errors
• Create a Custom Subroutine or Module
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
CGI.pm Documentation Page
• Always the Latest At:
http://stein.cshl.org/WWW/CGI/
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Simple CGI.pm Program
#! /usr/bin/perl
use warnings;
use strict;
use CGI qw(-no_xhtml);
my $q = CGI->new;
print $q->header(-type => 'text/html');
print $q->start_html(-title => 'Hello world!');
print $q->p('Hello world!');
print $q->end_html;
exit;
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Adding CSS Control
#! /usr/bin/perl
use warnings;
use strict;
use CGI qw(-no_xhtml);
my $q = CGI->new;
print $q->header(-type => 'text/html');
print $q->start_html(-title => 'Hello world!',
-style => { -src => '/~diverdi/style/main.css' });
print $q->p('Hello world!');
print $q->end_html;
exit;
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Adding JavaScript Support
#! /usr/bin/perl
use warnings;
use strict;
use CGI qw(-no_xhtml);
my $q = CGI->new;
print $q->header(-type => 'text/html');
print $q->start_html(-title => 'Hello world!',
-style => { -src => '/~diverdi/style/main.css' },
-script => { -type => 'text/javascript',
-src=>'javascript/verify });
print $q->p('Hello world!');
print $q->end_html;
exit;
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Random Image Display
• Program Delivers Image to Browser– Chosen From Designated Directory– Chosen At Random
• See Web Site For Code
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Image Selector
• Program Delivers Image to Browser– Chosen From Designated Directory– Chosen By Viewer's Selection– Not The Same As HTML Page
• List Of Links Varies According To Files In Directory
• See Web Site For Code
Intermediate CGI & CGI.pmWebmaster II - Fort Collins, CO
Copyright © XTR Systems, LLC
Survey System
• Program Maintains & Displays Survey Data– Accepts Viewer Data Submitted Through FORM– Maintains Data In Disk File On Server– Displays Results in General-Purpose Format– Displays Results in Form-Specific Format– Consists of Several, Separate Programs
• Programmer's Task Is to Synchronize Them
• See Web Site For Code