The University of Texas at Austin- What Starts Here Changes the World
Services Navigation


Putting It All Together

Taking all the things learned so far, this script builds a customized response based on form input and environment variables, and saves the form input to a tab-delimited text file.

Open a text editor to write the script:

pico together.cgi

Type the following Perl code in your text editor exactly as it appears here:

#!/usr/local/bin/perl
use CGI;
CGI::ReadParse(*in);
$file = '../tutorial_files/responses.txt';
print "Content-type:text/html\n\n";
print '<HTML><HEAD><TITLE>together.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
print '<h2>Thank You!</h2>';
print "<p>Thank you, $in{name}, for filling out our form.";
print "I hope your Web browser $ENV{HTTP_USER_AGENT}";
print " doesn't crash as often for you as it did for me.";
print '</BODY></HTML>';
open(FILE, ">>$file")||die "Can't open $file";
flock(FILE, 2)||die "Can't lock $file";
foreach $i (keys %in) {
       print FILE "$in{$i}\t";
}
print FILE "\n";
close(FILE) || die "Can't close $file";

Save the file and exit the editor.

Make the script executable, type:

chmod 755 $HOME/public_html/cgi-bin/together.cgi

This table includes a line-by-line explanation of together.cgi:

#!/usr/local/bin/perl Tells the server the Perl interpreter is located in the directory /usr/local/bin/perl
use CGI;
CGI::ReadParse(*in);
Loads the CGI module, and calls the ReadParse method from the CGI module.
$file = '../tutorial_files/responses.txt'; Assigns the path of the file used to store the form information to the variable $file.
print "Content-type:text/html\n\n";
print '<HTML><HEAD><TITLE>together.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
print '<h2>Thank You!</h2>';
print "<p>Thank you, $in{name}, for filling out our form.";
print "I hope your Web browser $ENV{HTTP_USER_AGENT}";
print " doesn't crash as often for you as it did for me.";
print '</BODY></HTML>';
These print statements create the HTML page response, using elements of the %in and %ENV hashes.
open(FILE, ">>$file")||die "Can't open $file";
flock(FILE, 2)||die "Can't lock $file";
foreach $i (keys %in) {
       print FILE "$in{$i}\t";
}
print FILE "\n";
close(FILE) || die "Can't close $file";
Opens the responses.txt file, creates an exclusive lock for responses.txt, prints tab-delimited file information to responses.txt, then closes responses.txt.

 

 

 


  Updated 2006 August 14
  Comments to TeamWeb