
| Linuxtopia Contents |
Perl Tutorial - An Introduction to Perl - CGI | |||
| [an error occurred while processing this directive] |
Contents14. Simple CGIFor an introduction to Common Gateway Interface, see https://hoohoo.ncsa.uiuc.edu/cgi/ and https://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html. Example: Server Status ReportLet's start with a CGI program that takes no input but produces output. The following Perl program reports the load on the Web server, using the standard Unix commands "hostname", "uptime", and "w". The output would look something like this:
Here's the program that produced it: #!/usr/local/bin/perl
# Send error messages to the user, not system log
open(STDERR,'<&STDOUT'); $| = 1;
# Headers terminate by a null line:
print "Content-type: text/html\n\n";
$host = `hostname`;
chop $host;
$uptime = `uptime`;
$w = `w -s -h`;
print <<BUNCHASTUFF;
<HTML><HEAD>
<TITLE>$host Status</TITLE>
</HEAD><BODY>
<H1>What's Happening at $host</H1>
$uptime
<PRE>$w</PRE>
<HR>
</BODY></HTML>
BUNCHASTUFF
exit;
Suppose user "bitman" wants to store this program on MU's SHOWME and SGI webservers, which use the "Apache" server software, configured for "CGI anywhere". If bitman doesn't already have a web directory, he should create one with these Unix commands: mkdir ~/www; chmod a+x ~ ~/wwwThen the above program could be put in a file ~/www/status.cgi, and that file made readable and
executable: chmod a+rx ~/www/status.cgi On the SHOWME server, the program would then be referenced as:
Example: Web FormHere is the image of a World Wide Web Electronic Form:
When something is entered and the submit button pressed, here is a resulting screen:
Perl Program to Generate and Process FormHere is the Perl program that generates both the form and the response. It uses an external module called "cgi-lib.pl" that is available from network Perl archives such as https://cgi-lib.stanford.edu/ #!/usr/local/bin/perl
# Send error messages to the user, not system log
open(STDERR,'<&STDOUT'); $| = 1
require "cgi-lib.pl"; # Get external subroutines
print &PrintHeader;
$script = $ENV{'SCRIPT_NAME'};
$webserver = $ENV{'SERVER_NAME'};
if (! &ReadParse(*input)) { &showform }
else { &readentries }
exit;
sub showform {
# If there is no input data, show the blank form
print <<EOF;
<HTML><HEAD>
<TITLE>Form Example, Part 1</TITLE>
</HEAD><BODY>
<H1>Web Form Example</H1>
<P>(From https://$webserver$script)
<FORM METHOD="POST"
ACTION=$script>
<PRE>
Enter your ID Number: <INPUT NAME=idnum>
Enter your Name: <INPUT NAME=name>
Select favorite Color: <SELECT NAME=color>
<OPTION>red<OPTION>green<OPTION>blue
</SELECT>
</PRE>
To submit the query, press this button:
<INPUT TYPE=submit VALUE="Submit Request">
</FORM>
</BODY></HTML>
EOF
} # End of sub showform #
sub readentries {
# Input data was detected. Echo back to form user.
print <<EOF;
<HTML><HEAD>
<TITLE>Form Example, Part 2</TITLE>
</HEAD><BODY>
<H1>Results of Form</H1>
<P>(From https://$webserver$script)
<P>Your ID Number is $input{'idnum'}, your name is $input{'name'},
and your favorite color is $input{'color'}.
<HR>
[<A HREF=$script>Try again</A>]
EOF
} # end of sub readentries #
|