Examples in Perl

Perl and CGI

How do you generate HTML content using Perl and process HTML forms ?
The recomended method is to use the CGI.pm module. Reffer to the documentation on this module at http://search.cpan.org/~lds/CGI.pm-3.03/CGI.pm. A good tutorial is available at http://home.school.net.hk/~hywong/cgipmtut.html

Here is another method you could use (url examples follow) :

To start output of a HTML file, use :

print "Content-type: text/html", "\n\n";
print "";

This is the header that indicates what type of data follows (the text/html is the mime type)

The following script accepts data from a form and displays it :

#!/usr/bin/perl

# Whether the method used is GET or POST, store the parameters passed in $QueryString
if($ENV{'REQUEST_METHOD'} eq "POST") {
  read(STDIN, $QueryString, $ENV{'CONTENT_LENGTH'});
} else {
  $type = "display_form";
  $QueryString = $ENV{QUERY_STRING};
}

# split the QueryString at &'s to give you a list with each element with a format like "name=value"
@NameValuePairs = split (/&/, $QueryString);

# Split each NameValue pair, replace +'s by spaces, get the character equivalent of any data in hex (%XX) and finally, store it in an associative array
foreach $NameValue (@NameValuePairs) {
  ($Name, $Value) = split (/=/, $NameValue);
  $Value =~ tr/+/ /;
  $Value =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C",hex ($1))/eg;
  $Form{$Name} = $Value;
}

print "Content-type: text/html", "\n\n";
print "<html><body>";

foreach $Form_key (keys %Form) {
    print "$Form_key = $Form{$Form_key}<BR>"
}

print "</body></html>";

Lets use the following HTML form to call the above script :


<form method="GET" action="http://www.motreja.com/cgi-bin/trial.pl">
<input type=text name=name1><BR>
<input type=text name=name2><BR>
<input type=text name=name3><BR>
<input type=submit>
</form>

If we enter <example text> in the first text box, % in the second text box and ? in the third text box, the url becomes :

http://www.motreja.com/cgi-bin/trial.pl?name1=%3Cexample+text%3E&name2=%25&name3=%3F

As you can see, some characters are encoded.
The first part of the url gives the script to execute.
It is followed by a "?"
Then, we have name value pairs in the form name=value, seperated from each other using &'s
The name valur pairs here are : name1=%3Cexample+text%3E, name2=%25, name3=%3F.
"<" is encoded as %3C, ">" is encoded as %3E, "%" is encoded as %25, "?" is encoded as %3F

Now, we can look at how the script decodes this :

Firstly, in this example, we used the GET method.
If we had used the POST method, only the url to the script would show up in the browser window and the parameters would be passed to the script on STDIN.

In any case, the first part of the script copies this information (from STDIN for POST method and from the environment variable $ENV{QUERY_STRING} in case of GET method) into $QueryString.
Next, we split at &'s to get the name valur pairs into @NameValuePairs.

Now, for each name value pair, we replace +'s by spaces and anything of the form %(two hex digits) is converted to its character equivalent.
Then, we store these name value pairs in an associative array %Form.

Finally, we print each name value pair (afte printing the HTTP header)

NOTE: This examle assumes you are using only alphabets in the name part. If you are planning to use special characters here too, you should process $Name similar to $Value.


Back to Table of Contents