Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,

Similar presentations


Presentation on theme: "Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,"— Presentation transcript:

1 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi, Ph.D., MBA

2 Intermediate CGI & CGI.pm Webmaster 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

3 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI In Brief

4 Intermediate CGI & CGI.pm Webmaster 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

5 Intermediate CGI & CGI.pm Webmaster 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

6 Intermediate CGI & CGI.pm Webmaster 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

7 Intermediate CGI & CGI.pm Webmaster 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

8 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC Simple CGI Program in Perl #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " \nHello world!\n \n"; print " \n"; print "Hello world!\n"; print " \n"; exit; Create a File Named "simple.pl"

9 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC Simple CGI Program in Perl #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " \nHello world!\n \n"; print " \n"; print scalar(localtime(time())), "\n"; print " \n"; exit; Create a File Named "time.pl"

10 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC Escaping Quotes #!/usr/bin/perl print "Content-type: text/html\n\n"; print " "; print " Hello world! "; print " "; print scalar(localtime(time())), "\n"; print " "; exit; Modify time.pl Escaping Will Get Old Fast

11 Intermediate CGI & CGI.pm Webmaster 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( \n); print qq( \nHello world!\n \n); print qq( \n); print scalar(localtime(time())), "\n"; qq(Hello world!\n); print qq( \n); exit; Called "Double Quote" Operator

12 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC "Here" Documents # !/usr/bin/perl print <<__END_OF_HTML__; Content-type: text/html hello world! Hello world! __END_OF_HTML__ exit; Be Careful of Ending Delimiter

13 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC Multiple Delimiters # !/usr/bin/perl print <<__HTML_1__; Content-type: text/html hello world! __HTML_1__ print scalar(localtime(time())), "\n"; print <<__HTML_2__; __HTML_2__ exit;

14 Intermediate CGI & CGI.pm Webmaster 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

15 Intermediate CGI & CGI.pm Webmaster 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

16 Intermediate CGI & CGI.pm Webmaster 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

17 Intermediate CGI & CGI.pm Webmaster 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

18 Intermediate CGI & CGI.pm Webmaster 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

19 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI.pm Documentation Page Always the Latest At: http://stein.cshl.org/WWW/CGI/

20 Intermediate CGI & CGI.pm Webmaster 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;

21 Intermediate CGI & CGI.pm Webmaster 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;

22 Intermediate CGI & CGI.pm Webmaster 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;

23 Intermediate CGI & CGI.pm Webmaster 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

24 Intermediate CGI & CGI.pm Webmaster 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

25 Intermediate CGI & CGI.pm Webmaster 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

26 Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC Survey System System Programs –Submit Survey –Create Simple Report –Create Fancy Report Each Program Is Installed in CGI Directory Programs Are Accessed Through HTML –Developer's Choice on Specifics


Download ppt "Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,"

Similar presentations


Ads by Google