|  
[Webscripting Homepage] | [Javascript Homepage] | [Perl/CGI HomePage]
 
 Basic use of CGI.pm module
 
 
CGI.pm has a ton of methods for generating HTML script along with handling 
output for a form, the following is an example, with comments.
 
#!/usr/local/bin/perl -w  
#Note you should normally use the -T option for CGI scripts 
#This is an odd CGI program in that it creates a form that points to 
#itself as an action 
#THIS IS NOT THE USUAL WAY TO DO THINGS (ie the self generating form)
#use warnings; 
#use strict; 
#invoke the CGI.pm module 
use CGI qw( :standard ); 
 
$JSCRIPT=<<END; 
//Make sure not to get trapped in frames 
if (window != top) top.location.href = location.href; 
END 
 
#use CGI.pm functions to print the  
#Content-type: text/html\n\n line 
#and to set the bgcolor, title properties  
#and insert a javascript 
print header(), start_html( -title => "Test of CGI.pm", 
-BGCOLOR => "skyblue", -script => $JSCRIPT);  
#Get a value from the form 
my $word = param( "favword" ); 			    
#This part uses a regular expression to make sure 
#nothing nasty is done 
$word =~ /(^\w+$)/; 
$word = $1; 
print p( 'Enter one of your favorite words here: ' ); 
#Here is the odd part, where the cgi program  
#makes it's own form 
#Though there are times when this 
#makes sense, it is not the "norm" 
print start_form( -method => "post", -action => "cgi-bin/cgipm.pl" ); 
print textfield( "favword" ); 
print submit( "Submit Word" ), end_form();
 
print p( 'Your word is: ', b( $word ) ) if $word; 
print end_html(); 
 
For more details check out CGI.pm 
Perl5 library  
This page last updated on:
 
    |