The CGI.pm Format
The most common task of a CGI program is to create the required HTML tags.
Forms, buttons, lists
A standard set of routines has been created
CGI.pm is a Perl module of functions for such common tasks
CGI.pm is part of the standard
Perl distribution
perldoc CGI
-
The use declaration in Perl specifies that it needs access to a particular module
use CGI ":standard";
-
Common CGI.pm functions
<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<!-- popcorn.html - This describes popcorn sales form page -->
<html>
<head>
<title> Popcorn Sales CGI program </title>
</head>
<body>
<!-- The next line gives the address of the CGI program -->
<form action = "http://personal.utulsa.edu/~diaz/cgi-bin/popcorn.pl"
method = "POST">
<h2> Welcome to Millennium Gymnastics Booster Club Popcorn Sales </h2>
<table>
<!-- Text widgets for name and address -->
<tr>
<td> Buyer's Name: </td>
<td> <input type = "text" name = "name" size = "30"> </td>
</tr>
<tr>
<td> Street Address: </td>
<td> <input type = "text" name = "street" size = "30"> </td>
</tr>
<tr>
<td> City, State, Zip: </td>
<td> <input type = "text" name = "city" size = "30"> </td>
</tr>
</table>
<br />
<table border = "border">
<!-- First, the column headings -->
<tr>
<th> Product Name </th>
<th> Price </th>
<th> Quantity </th>
</tr>
<!-- Now, the table data entries -->
<tr>
<td> Unpopped Popcorn (1 lb.) </td>
<td> $3.00 </td>
<td> <input type = "text" name = "unpop" size ="2"> </td>
</tr>
<tr>
<td> Caramel Popcorn (2 lb. canister) </td>
<td> $3.50 </td>
<td> <input type = "text" name = "caramel"
size = "2"> </td>
</tr>
<tr>
<td> Caramel Nut Popcorn (2 lb. canister) </td>
<td> $4.50 </td>
<td> <input type = "text" name = "caramelnut"
size = "2"></td>
</tr>
</tr>
<td> Toffey Nut Popcorn (2 lb. canister) </td>
<td> $5.00 <</td>
<td> <input type = "text" name = "toffeynut"
size = "2"> </td>
</tr>
</table>
<br />
<!-- The radio buttons for the payment method -->
<h3> Payment Method: </h3>
<input type = "radio" name = "payment" value = "visa" checked> Visa <br />
<input type = "radio" name = "payment" value = "mc"> Master Card <br />
<input type = "radio" name = "payment" value = "discover"> Discover <br />
<input type = "radio" name = "payment" value = "check"> Check <br /> <br />
<!-- The submit and reset buttons -->
<input type = "submit" value = "Submit Order">
<input type = "reset" value = "Clear Order Form">
</form>
</body>
</html>
popcorn HTML form
#!/usr/local/bin/perl -w
# This is popcorn.pl
# It is a CGI program to process the popcorn sales form
# Initialize a hash of products and their prices, total price,
# and total number of purchased items
%prices = ("unpop" => 3, "caramel" => 3.5, "caramelnut" => 4.5,
"toffeynut" => 5);
$total_price = 0;
$total_items = 0;
# Produce the header part of the HTML return value
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<title> CGI-Perl Popcorn Form </title></head> \n";
print "<body>\n";
print "Thank you for your order. <br /> <br />\n";
# Determine the request method and get the query string
$request_method = $ENV{'REQUEST_METHOD'};
if ($request_method eq "GET") {
$query_string = $ENV{'QUERY_STRING'};
}
elsif ($request_method eq "POST") {
read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
}
# Split the query string into the name/value pairs
@name_value_pairs = split(/&/, $query_string);
# Loop to process the array of item names and quantities
foreach $name_value (@name_value_pairs) {
# Split the pairs into names and values and translate the values
# into text by decoding the hex characters
($name, $value) = split (/=/, $name_value);
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg;
# Add the number of these items to the total items ordered,
# compute the cost of the item, and add its cost to the total price,
$total_items += $value;
$cost = $prices{$name} * $value;
$total_price += $cost;
} ##- end of foreach
# Produce the result information to the browser and finish the page
print "You ordered $total_items popcorn items <br /> <br /> \n";
print "Your total bill is: \$ $total_price <br /> \n";
print "</body> </html> \n";