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
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

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





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 and

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       3  pico
    c676828  ftp [email protected]         -
    ccgreg   q1  monad.missouri         tcsh
    c552997  q5  mizzou-ts2.mis         telnet

Here's the program that produced it:

    # 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;
    <TITLE>$host Status</TITLE>
    <H1>What's Happening at $host</H1>

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:

Example: Web Form

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

Web Form Example


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


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 "" that is available from network Perl archives such as
    # Send error messages to the user, not system log
    open(STDERR,'<&STDOUT');  $| = 1
    require "";  # Get external subroutines
    print &PrintHeader;
    $script = $ENV{'SCRIPT_NAME'};
    $webserver = $ENV{'SERVER_NAME'};
    if (! &ReadParse(*input)) { &showform }
    else { &readentries }

    sub showform {
    # If there is no input data, show the blank form

    print <<EOF;
    <TITLE>Form Example, Part 1</TITLE>
    <H1>Web Form Example</H1>
    <P>(From https://$webserver$script)
    Enter your ID Number:  <INPUT NAME=idnum>
    Enter your Name:       <INPUT NAME=name>   
    Select favorite Color: <SELECT NAME=color>
    To submit the query, press this button: 
    <INPUT TYPE=submit VALUE="Submit Request">
    } # End of sub showform #
    sub readentries {
    # Input data was detected.  Echo back to form user.

    print <<EOF;
    <TITLE>Form Example, Part 2</TITLE>
    <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'}.
    [<A HREF=$script>Try again</A>]
    } # end of sub readentries #

[an error occurred while processing this directive]