Web programming with Perl

In this lecture we will discuss how to use Perl for server side scripts. Practically eny language can be used for server side scripting if the script follows some rules that define the protocol of information exchange between the server and the application (script). The set of these rules is called CGI (Common Gateway Interface). A CGI program is any program designed to accept and return data that conforms to the CGI specification.

CGI

A CGI script passes information to the web server through its own STDOUT. Basically, everything that your scripts prints to the standard output is going to be passed to the web server and then tranferred to the web client. You need to remember, though, that this output has to be in the form of HTTP response. In the simplest form, it has to have a line like

Content-type: text/html
followed by an empty string and then the actual text of the HTML page your script wants to generate.

Here is a simple example of the Perl script that does it:

print "Content-type: text/HTML\n\n";
print "<html><body>Server side Perl script works!</body?</html?";

If the script needs to receive some information from the web client than this information gets transferred to the web server and then the server pass it to the script. Depending on the method used by the client (GET or POST) the server uses different ways to pass the data to the script. If the web form uses the GET method, then the server sets the environment variable QUERY_STRING that contains the whole string passed by the web form. Please see example.

print "Content-Type: text/html\n\n";

print "<h3>Receiving info sent with using the GET method</h3>";
print "<li>Query string: $ENV{QUERY_STRING}\n";

If the client uses method POST to send the data, the server delievers the data to the standard input of the CGI script. In addition to this, the server sets variables CONTENT_TYPE and CONTENT_LENGTH. These variables can be accessed by a Perl script through the %ENV hash. Please see example.

print "Content-Type: text/html\n\n";

print "<h3>Receiving info sent with using the POST method</h3>";
print "<li>Content type: $ENV{CONTENT_TYPE}\n";
print "<li>Content length: $ENV{CONTENT_LENGTH}\n";

print "<li>Data: <pre>";
for($i=0;$i<$ENV{CONTENT_LENGTH};$i++){
  $key = getc(STDIN);
  print "$key";
}
print "</pre>";

Perl CGI module

Theoretically the problem of analyzing the script input (either from the environment variables or from the STDIN) and retrieving the client form information is not difficult, but in real life it's not that easy. That's why it's much more safe to use the special Perl module from Lincoln D. Stein - CGI.pm. This module provides not only convenient tool to extract form parameters, but also a plenty of other functions that really helps to build a good HTML output.

In order to use this module we need to include in our Perl script the following line:

use CGI ":standard";
which means that we would like to use non-object oriented interface (standard) from the Perl Module (pm) named CGI.

The most useful (for this class) functions of the module are:
Function What for Arguments Example
header() Returns the HTTP header line like Content-Type: text/html The following optional nmaed arguments are recognized:
  • -type
  • -status
  • -expires
  • -cookie
  • -charset
  • -attachment
  • print header(-type=>'image/jpeg', -expires=>'+7d');
    start_html() Prints the header of the HTML document The following parameters are optional:
  • -title - the title
  • -author - the author's e-mail address
  • -xbase - includes a <BASE> tag that points to some external location.
  • -target
  • -meta - defines one or more tags
  • -style - defines a cascading stylesheet
  • -script - defines Netscape JavaScript functions to incorporate into the HTML page
  • -onLoad and -onUnload parameters to register JavaScript event handlers
  • print start_html(-title=>"Perl CGI script");
    end_html() End the body of HTML document  
    print end_html;
    param() Returns the value of the parameter received from the client or the names of all parameters (if used without an argument) name of the parameter
    $name = param("name");

    Using these functions we can easily create a Perl script that prints all parameters received from the client:

    use CGI ":standard";
    
    print header;
    print start_html(-title=>"Show parameters");
    
    print <<TABLE_HEADER;
    <table align="center">
    <tr>
     <th>Parameter <th>Value
    </tr>
    TABLE_HEADER
    
    @params = param();
    for( @params ){
      print "<tr> <th align="right">$_</th> <td><em>". param($_) ."</em></td> </tr>";
    }
    print "</table>\n";
    
    print end_html;
    
    Please see example.


    For more information on the subject please see
  • CGI.pm - a Perl5 CGI Library
  • CGI: Common Gateway Interface