Simple Perl Example
Processing Data Collected by Forms

Prior to the advent of PHP, Perl was a very common language used to process the information passed from an HTML Form. The Perl programs were not allowed to be in any directory, but needed to be placed in a subdirectory off of web-docs called cgi-bin. Perl is a very powerful language, and is used frequently for scripts that handle UNIX system routines. Perl can be very cryptic to read at times, which adds to the interest in the language by some programmers. For mor information on Perl, check out Mr. Hyatt's Perl Page.

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 Basic Form Program   form.html

This is a simple HTML document that generates a form. After the user inputs information, the form calls another CGI program called process.pl that uses the input and generatess another web page. HTML programs can reside in the web-docs directory on any sub-directory under there. The perl programs must reside in the cgi-bin directory or a subdirectory below that entry point.

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>



Form Processing Program   process.pl

This program reads in the data provided by form.html and creates a web page using that information. The program is written in perl, and must be placed in the cgi-bin directory. The permissions on the file must be set properly so that the program is executable.

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
	


Click here to run the form program and see the output: