The examples shown below represent two programs. The first one is a simple HTML Form that calls the second one when the submit button is pressed.
The following program asks the user to input a name as text and slect among
three options. After the submit button is pressed, the control and arguments are passed to the perl routine to process.
<HTML> <BODY BGCOLOR="#FFFFFF"> <H1>Basic Form </H1> <FORM ACTION=/~dhyatt/cgi-bin/process.pl METHOD=POST> Please Enter Your Name: <INPUT TYPE=TEXT NAME="name" LENGTH=30> <P> How do you like to be evaluated? <SELECT NAME = "choice" > <OPTION SELECTED> Quizzes <OPTION> Projects <OPTION> Tests </SELECT> <P> <INPUT TYPE=SUBMIT VALUE="Submit Form"> </FORM> </BODY> </HTML>
The program uses a CGI Perl module that has certain functions, one of which is &ReadParse. That function grabs the data that was passed by the form, and puts it into something called an associative array, also known as a hash. The hash, %in, can allow the new web page to find the values of the passed variables by referencing keys relating to the variable names. The command $in{ $key } would return the value of the variable associated with that key.
To print out the web page itself, much of the standard HTML code looks
the same, but is actually encased in a long print statement.
In perl, the print statement can print multiple lines until it reaches
some label. Things that require computation in perl are located
outside the print statements.
#!/usr/bin/perl # Program Name: process.pl # Author: D. W. Hyatt # This program uses the data passed by the form program and creates # a new web page with that information. use CGI qw(:cgi-lib :standard); # Use CGI modules that let people read data passed from a form &ReadParse(%in); # This grabs the data passed by the form and puts it in an array $name = $in{"name"}; # Get the user's name and assign to variable $preference = $in{"choice"}; # Get the choice and assign to variable # Start printing HTML document print<<EOSTUFF; Content-type: text/html <HTML> <BODY BGCOLOR=WHITE TEXT=BLACK> <H1> Hello, $name </H1> <!-- Use variables in HTML text --> You prefer $preference. <BR> EOSTUFF for ($i=1; $i<=5; $i++) # Print name 5 times {print "$i. $name <BR>";} print<<EOF; <!-- Finish up document --> </BODY> </HTML> EOF