Logo         Community
  Trog


Copyright notice:

This article is copyright Melonfire, 2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein is proprietary to Melonfire, 2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=21



Perl 101 (part 7): CGI Basics
An introduction to the basics of CGI scripting, and a new type of Perl variable.

Moving On

If you've been paying attention these last few weeks, you should now know enough to write basic Perl programs. And with that task accomplished, Perl 101 now turns its attention to teaching you how to use Perl to generate HTML pages dynamically through server-side CGI scripts.

Since CGI scripts typically involve passing a number of name-value pairs of variables from one page to another, we're first going to introduce you to a new type of variable, and then use that knowledge to build some simple CGI scripts. Keep reading!

Heroes Of The Silver Screen

Here's a simple program that demonstrates how hashes can be used:


#!/usr/bin/perl

# define a hash
%director = ("1995" => "Mel Gibson", "1996" => "Anthony Minghella", "1997" => "James Cameron", "1998" => "Steven Spielberg", "1999" => "Sam Mendes");

# print
print "The Best Director Oscar in 1995 went to $director{1995}\n";
print "The Best Director Oscar in 1996 went to $director{1996}\n";
print "The Best Director Oscar in 1997 went to $director{1997}\n";
print "The Best Director Oscar in 1998 went to $director{1998}\n";
print "The Best Director Oscar in 1999 went to $director{1999}\n";


And here's the output:


%myhero = ("fname", "Donald", "lname", "Duck");


In the example above, a hash has been used to store a bunch of name-value pairs, and a print() function has been used to display them.

You can also use the alternate hash notation if you prefer.


# define a hash
%director = ("1995", "Mel Gibson", "1996", "Anthony Minghella", "1997", "James Cameron", "1998", "Steven Spielberg", "1999", "Sam Mendes");


Each() Time The Lights Go Out...

Perl also has the each() function, designed to return a two-element array for each key-value pair in the hash. This comes in particularly useful when iterating through the hash, and is conceptually similar to the foreach() loop. Take a look:


#!/usr/bin/perl

# define movie hash
%film = (1995 => 'Braveheart', 1996 => 'The English Patient', 1997 => 'Titanic', 1998 => 'Saving Private Ryan', 1999 => 'American Beauty');

# define director hash
%director = ('Braveheart' => 'Mel Gibson', 'The English Patient' => 'Anthony Minghella', 'Titanic' => 'James Cameron', 'Saving Private Ryan' => 'Steven Spielberg', 'American Beauty' => 'Sam Mendes');

# use loop to iterate through hash
while (($year, $filmname) = each %film)
        {
        print "The Oscar for Best Director($year) goes to $director{$filmname} for $film{$year}\n";
        }


In the example above, we've created two hashes, one for the movies and years, and the other linking the movies with their directors. Next, we've used the each() function to assign the name-value pairs from the first hash to two variables, $year and $filmname, which are then used to obtain the corresponding director names from the second hash.

Here's the output:


%myhero = ("fname", "Donald", "lname", "Duck");


Note that Perl allows you to use scalars within the hash notation as well - $director{$filmname} above is an example of this.

And finally, the delete() function allows you to delete a pair of elements from the hash. For example, if you have the hash


%film = (1995 => 'Braveheart', 1996 => 'The English Patient', 1997 => 'Titanic', 1998 => 'Saving Private Ryan', 1999 => 'American Beauty');


you can delete the second entry(1996) like this:


delete $film{1996};


And your hash will then look like this:


%film = (1995 => 'Braveheart', 1997 => 'Titanic', 1998 => 'Saving Private Ryan', 1999 => 'American Beauty');


A Cure For Low Self-Esteem


#!/usr/bin/perl

print "Content-Type: text/html\n\n";
print "<html><body><h1>God, I'm good!</h1></body></html>";


And when you view this page in your browser (assuming you've placed it in the appropriate place with the appropriate permissions, and that the file is called "good.cgi") by surfing to http://localhost/cgi-bin/good.cgi, you'll see this:


%myhero = ("fname", "Donald", "lname", "Duck");


Let's dissect this a bit. The first line of the script is one you'll have to get used to seeing in all your CGI scripts, as it tells the browser how to render the data that follows. In this case, we're telling the browser to render it as HTML text.

Note the two line breaks following the Content-Type statement - forgetting these is one of the most common mistakes Perl newbies make, and it's the cause of a whole slew of error messages.

Our next line is a print() statement which simply outputs some HTML-formatted text - this is what the browser will see.

You can also use variables when generating your page - as the next example demonstrates:


#!/usr/bin/perl

print "Content-Type: text/html\n\n";
print "<html><body><h1>Tables</h1>";
print "<table border=2>";

for ($x=1; $x<=5; $x++)
{
print "<tr><td>Row $x</td></tr>";
}

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


GETting Your Form To Work

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:


http://localhost/cgi-bin/readform.cgi?name=Luke+Skywalker


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!

Note: All examples in this article have been tested on Linux/i586 with Perl 5.005_03. YMMV!


Copyright notice:

This article is copyright Melonfire, 2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein and proprietary to Melonfire, 2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=21



Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback