Apr 262012
 
Article Perl

(Leer este artículo en español)

A CGI script needs to have access to the information that the web server provides about the request being served:

    • The host name, url and arguments (query string) of the request that triggered the execution of the script.
    • The applicable cookies.
    • The parameters and their values, in the case of a POST request from a form .

The script must perform the intended processing and generate a valid HTML code, including the HTTP headers. A basic example of a CGI script written in Perl is:

Argument passing

The CGI protocol states that the communication of information between the web server and the script is performed by means of a series of environment variables: HTTP_HOST, SCRIPT_URI, QUERY_STRING,… In Perl, these variables are accesible by means of the %ENV  hash. If we modify the above script to print to screen a dump of the content of that variable:

and execute this script with the url:

we will get the following output:

Module CGI.pm

There is a CGI.pm Perl module availabe from CPAN that simplifies the programming of CGI scripts. This module encapsulates the functionality required to write a CGI script. Among other features, it parses the recived arguments and makes their values accesible through the ‘param’ method.

Index of posts related to Perl programming

 Posted by at 8:56 pm

 Leave a Reply

(required)

(required)