Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Contents

1. What Is Perl?
2. Course Requisites and Goals
3. Perl References & Resources
4. State of Perl
5. Taste of Perl
6. Storing & Running Perl Programs
7. The Elements
8. Literals & Operators
    9. Loops and I/O
10. Grade Book Example
11. Pipe I/O and System Calls
12. Matching
13. Parsing
14. Simple CGI
15. Testing Perl Programs
16. Common Goofs

14. Simple CGI

For 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 Report

Let'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:

What's Happening at sgi1
7:09pm up 1 day, 18:38, 4 users, load average: 0.08, 0.21, 0.53
    c676828  q0  128.206.58.85       3  pico
    c676828  ftp [email protected]         -
    ccgreg   q1  monad.missouri         tcsh
    c552997  q5  mizzou-ts2.mis         telnet

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 ~ ~/www
Then 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: https://www.missouri.edu/~bitman/status.cgi

Example: Web Form

Here is the image of a World Wide Web Electronic Form:

Web Form Example

(From https://some.webserver.missouri.edu/~user/myform.cgi)

Enter your ID Number:  
Enter your Name:          
Select favorite Color: 
To submit the query, press this button:

When something is entered and the submit button pressed, here is a resulting screen:

Results of Form

(From https://some.webserver.missouri.edu/cgi-bin/myform)

Your ID Number is 196965, your name is G. K. Johnson, and your favorite color is green.
_____________________________________________________

[Try again]

Perl Program to Generate and Process Form

Here 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 #


 
 
[an error occurred while processing this directive]