Chapter 17 – Perl CGI Introcution

Perl’s gateway to cyberspace is CGI (Common Gateway Interface). Perl scripting is for scripting for your own computer, CGI scripting is for web servers and web sites.

The main difference between Perl and CGI (though CGI in essence is just a series of Perl modules) is CGI is generally object oriented. If the thought of using objects frightens you, rest assured you’re not required to use that format.

When you get into using different modules, many modules are object-oriented so you’ll need to look up the documentation before you’ll be able to use their code.

Initial script setup

To make your Perl scripts CGI, you use the CGI module

use CGI qw/:standard/;

The top section of your scripts would look something like

#!/usr/bin/perl

use warnings;
use strict;
use CGI qw/:standard/;

The first thing you need to know when using CGI (remember there are more than one way to do things. There are other ways to use the CGI module or get around it completely by typing out all the HTML for your web site) is the need to print out the HTTP Headers.

This is done by

print header, start_html();

Before you print() anything out to the browser you must print out the headers. It may be easier to put this code near the top of your script if your script allows it. If you attempt to print out information before the headers are set, you’ll get an Internal Server Error (ISE).

Your Perl-ish errors won’t work in CGI, if you have a missing semicolon on line 159 or you didn’t predeclare a variable name under the strict pragma. To turn on CGI warning we use fatalsToBrowser which will show us any and all Perl-ish errors. This will only catch errors that occur after you initialize the warnings which means if you setup warnings on line 7 and the error is on line 5, you’ll get an Internal Server Error (ISE) instead of a real error message.

Because of this, if you choose to use this while creating your CGI script it should be setup near the top of the script. Please note: when writing your script it is encouraged you use the fatalsToBrowser but once the script is live you should remove it. The error message could possibly give away too much server information to potential computer wannabe hackers.

#!/usr/bin/perl

use warnings;
use strict;

use CGI qw/:standard/;
use CGI::Carp ‘fatalsToBrowser’;

print header, start_html();


Printing to browser

Printing to the browser is no different than printing to the command line. You print() your strings using print “”; as you can see below. Any text or variables you decide to print will appear in the user’s browser.

The only difference here is your ability to output your data any way you wish. Since you can now use HTML, you can align your text using tables, make links, change font sizes and colors or even print out JavaScript as well.

print “Hello, CGI World!”;

If you’re not using CGI’s object oriented (OO) interface you’ll need to be careful not to nest unescaped quotes inside of quotes when you are printing out HTML. Take, for instance, assigning a font color. The HTML code would be :

print “<font color=”red”>Hello, CGI World!</font>”;

This would result in incorrect syntax. When printing out HTML there are three ways you could accomplish the same thing.

The first being print qq(). printqq() represents printing a quoted quote string. You nest your string in parenthesis so the quotes from the HTML won’t affect your script.

print qq( <font color=”red”>Hello, CGI World!</font>);

The second way of printing HTML to the screen is escaping the quotes. Using this method will often make your text less readable and harder to maintain so this is the least recommended. The escape character is the / slash. You place this in front of the nested quotes.

print “<font color=/”red/”>Hello, CGI World!</font>”;

The third and final way to print HTML without having to worry about quotes is using herdocs. This method is recommended if you have more than one line of output to print at a time.

A heredoc allows you to print as many lines as you want with only one print statement. You know longer need to use the enclosing quotes or semicolons when you use herdocs which makes this the fasted method to printout mass amounts of HTML or even text output.

You print to a filehandle which you create in caps. In our example we used ENDOFPRINT which could have been ENDOFFILE, EOF, FORM or even CAT. You can name this nearly whatever you want but keep it all A-Z characters for simplicity.

All lines between the print line and the line at the very bottom that contains just the ENDOFPRINT (or whichever filehandle you used) will be printed. Note: the closing filehandle must be on it’s own line with no trailing spaces and at least one full blank line below it.

print <<”ENDOFPRINT”;

<font color=”red”>Hello, CGI World!</font>
<center><h2>We are line #1</J2></center>
<table border=”1″ width=”50″>
<tr>
<td>I am inside a table!</td>
</tr>
</table>

ENDOFPRINT

 

Ending the CGI script

Once all the data is printed to the browser you need to close off the HTML tags. the print header, start_html() from earlier prints out the beginning HTML tag, all the HEAD information and the starting BODY tag.

When you’re done, you need to close off the BODY aznd the HTML tag. Like with everything else in Perl and CGI, there is more than one way to do it (TIMTOWTDI).

You can hard code the closures yourself within a basic print statement.

print “</body></html>”;

Or you could use the CGI function of end_html; neither are suggested over the other, it’s all a principle of coder’s preference.

print end_html;


Uploading the script

In order for CGI to work, the web server you are working on has to have CGI enabled. If they do, you should already have a cgi-bin directory within your main HTML directory. All of the CGI scripts you develop or install need to inside the cgi-bin folder.

Once the script is uploaded, you’ll need to chmod the file (set the file permissions) to 755. If you don’t know how to do this, consult your FTP client’s help menu. 655 isn’t enough permissions to run CGI scripts and 777 is giving away too many rights. Regardless of what the author of the script says, never set the permissions higher than 755.

 

Setting the header information

Many times the CGI script will be a standalone complete script. This means you may have to create more specific header information such as the title, meta content, header type, the CSS files, etc.

print start_html (
-title=>”title of your page goes here”,
-base=>”true”,
-target=>”_blank”,
-meta=>{“keywords” =>”keywords go here”,
“copyright” =>”copyright 1996 King Tut”,
“description”=>”descript info”},
-style=>{“src” =>”/location/of/css.css”}, -BGCOLOR=>”gray”);

Note this isn’t a complete list of the header information you can customize within CGI but this should give you. If you need more information, consult your favorite search engine and search “cgi.pm” tutorials.

The CGI attributes are named after the HTML equivalents so they are pretty self explanatory.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)