After spending the past few months understanding Perl'svariables, operators and functions, it's time to start writing Perlprograms for the Web. This week, Perl 101 introduces you to the basics ofCGI scripting, and also teaches you how to use a new type of Perl variable,the hash.
The CGI environment comes with a set of pre-defined variables that can assist in the task of developing server-side scripts. Here's a list of the important ones:
$ENV{REMOTE_ADDR} The address of the client machine $ENV{REMOTE_HOST} The host name of the client machine $ENV{REQUEST_METHOD} The method used by a form to request data $ENV{HTTP_USER_AGENT} The client browser $ENV{QUERY_STRING} The query string passed if the GET method is used
Our next few examples will give you some idea of how these, and similar variables, can be used within your scripts.
#! /usr/bin/perl
$ip = $ENV{REMOTE_ADDR};
$browser = $ENV{HTTP_USER_AGENT};
print "Content-Type: text/html\n\n";
print "<html><body><font face=Arial>Your IP address is $ip and your browser
is $browser</font></body></html>";
And now, when you browse to this page, you'll see some
information on your IP address and browser version.
Our next example demonstrates how a Perl/CGI script can be used to process data submitted via a form. Here's the form...
<html>
<head>
<basefont face=Arial>
</head>
<body>
<form action=http://localhost/readform.cgi method=GET>
Enter your first name: <input type=text length=20 name=name><br>
<input type=submit value=Submit>
</form>
</body>
</html>
...and here's the CGI script that receives and processes the
data.
#!/usr/bin/perl
# readform.cgi
# reads form data and generates a page
# for GET data
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$yourname=$ENV{'QUERY_STRING'};
}
# for POST data
else
{
$yourname = <STDIN>;
}
# Remove spaces if any
$yourname =~ s/\+/ /g;
# split form data and store in hash
%details = split (/=/, $yourname);
# generate page
print "Content-Type: text/html\n\n";
print "<html><body>";
while (($name, $value) = each %details)
{
print "Thank you for your submission, $value!\n";
}
print "</body></html>";
The script above will accept data from the HTML form and
store it in a variable called $yourname. The manner in which data is submitted by the form(GET or POST) decides the manner in which it is assigned to the variable; the QUERY_STRING environment variable is used to make this decision.
If the text entered into the form contains spaces, the spaces are replaced with + characters when the form is submitted - this needs to be reversed via a regex. For example, if you enter the name "Luke Skywalker" into the form, the URL string will look like this:
Next, the name-value pairs are split apart on the basis of the = sign, and are assigned to their respective places in the hash %details. This hash is then used to print the name in the result page, which is also generated by the same script.
Thus, the CGI environment can be used to accept data from one Web page, process it or transfer it to another, and generate a new page containing dynamically-generated output. This is the basis of using CGI to create dynamic Web sites.
In the next issue of Perl 101, we'll delve deeper into CGI, with a look at some simple CGI programs that can be used to track hits on your Web site, or store visitor comments. Don't miss it!