A Basic CGI Tutorial using Perl

Topics on common programming languages
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

A Basic CGI Tutorial using Perl

Post by Neo » Fri Feb 05, 2010 4:12 am

Overview
This section describes the creation of CGI scripts in the Perl programming language. You should be familiar with perl before continuing.

Perl (Practical Extraction and Report Language) is great for making CGI scripts. It's extremely sophisticated string parsing routines make it very easy to handle CGI input. What might take 10 lines of code to do in C++ or another language could take only one or two in Perl.

Making Perl scripts is very simple. The server automatically takes everything that would have gone to the screen and redirects it to the browser. So, to send text to the browser, you would simply code something like:

Code: Select all

print "Hello, World!"; 
No problem, eh? Well, it is a bit more complicated than that...

The content-type header
Every CGI script must first output a content-type header that specifies the MIME type of the data that is being output by the script. For instance, "text/plain" would be the MIME type if it is outputting text, or "text/html" if it is outputting an HTML webpage.

The actual code for the header looks like this:

Code: Select all

print "Content-type: text/plain\n\n"; 
Note the two newlines following the header: this tells the server where the header ends and where the data begins.

A complete cgi script
Armed with this knowledge, we can create our first functional CGI script:

Code: Select all

#!/usr/local/bin/perl print "Content-type: text/plain\n\n"; print "Hello, World!"; 
Yes, believe it or not, this is a real, bona fide CGI script, although it doesn't do much at this point. All it does is sends the text "Hello, World!" to the browser.

Now, let's spruce it up. Say we want to output not just "Hello, World!" in text, but why not make it pretty with some HTML formatting?

Code: Select all

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>CGI Test</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1><em>\n";
print "Hello, World!\n";
print "</em></h1>\n";
print "</body></html>\n";
Now we've outputted a real HTML page that the browser will recognize.

Using environment variables
Now, outputting an HTML page is all well and good, but why would you want to do so when you could just put the HTML page on the server?

When the server executes a CGI script, it first sets a number of environment variables that you can retrieve using the $ENV array. These will tell you a number of useful things. Have you ever seen one of those nifty pages that tells you your IP address?

Code: Select all

#!/usr/local/bin/perl

$host = $ENV{'REMOTE_HOST'};

print "Content-type: text/html\n\n";
print "<html>\n";
print "<body>\n";
print "<p>\n";
print "Hello, $host!";
print "</p>";
print "</html>"; 
You might also tell the user the current time, or even what web browser they're using (stored in the HTTP_USER_AGENT environment variable.)

Receiving data from a form
Okay, here's where we get into what makes a CGI script really usefu; receiving the data sent by a form on a webpage.

Using the POST method, a CGI script retrieves the form data from stdin. This means you can retrieve it with a simple read().

Now, the data comes in a specific format. Each HTML <input> field and its value is sent along. Let's say the following is the form in the webpage:

Code: Select all

<form method="post" action="http://path.to/my/cgi/program">
<input type="hidden" name="value1" value="test1">
<input type="hidden" name="value2" value="test2">
<input type="hidden" name="value3" value="test3">
<input type="submit">
</form> 
These are hidden input fields for the purpose of simplicity. The Forms section in the Advanced HTML area will tell you how to make more complex forms.

Anyway, when the user submits this, this is what gets put into stdin for the script:

Code: Select all

value1=test1&value2=test2&value3=test3 
The CGI script is then responsible for parsing this into data that it can use.

Knowing all this, here's a script that sends back what was sent to it:

Code: Select all

#!/usr/bin/local/perl

$length = $ENV{'CONTENT_LENGTH'};

read(STDIN,$data,$length);

print "Content-type: text/html\n\n";
print "<html>\n";
print "<body>\n";
print "<p>\n";
print "Hello! you sent $length bytes of data which read: <br>";
print $data;
print "</p>";
print "</body>";
print "</html>"; 
Parsing the form data
Here's where Perl's native string routines really come in handy; if you read through the C++ section, you'll recall that performing this task required a class consisting of a hundred or so lines of code. With Perl, it will take only about half a dozen.

Thus, here is my final example, which sends back the data sent to it in a nice, formatted way:

Code: Select all

#!/usr/bin/local/perl

$length = $ENV{'CONTENT_LENGTH'};

print "Content-type: text/html\n\n";
print "<html>\n";
print "<body>\n";
print "<p>\n";
print "Hello! You sent $length bytes of data, which contained the following values:\n";
print "</p>\n";
print "<ul>\n";

read(STDIN,$data,$length);

@rawitems = split(/&/,$data);

foreach $thisitem (@rawitems)
{
   ($name,$value) = split(/=/,$thisitem);
   print "<li>$name = $value\n";
}

print "</ul>\n";
print "</body>\n";
print "</html>\n"; 
Note that the foreach loop gets each item and splits it in to $name and $value. You could, at that point, do whatever you want with each item (or put them in an array for later use.) In this example, they are simply printed.

This should be everything you need to go on and create interactive & useful CGI scripts in Perl.
Post Reply

Return to “.Net & Other Programming”