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!";
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";
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!";
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";
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>";
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>
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
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>";
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";
This should be everything you need to go on and create interactive & useful CGI scripts in Perl.