Perl Quick Reference
Language Essentials for Perl as used in CGI
NOTE: This is a selected list of key elements of Perl, see the Perl Reference Guide for a complete list [or book: Learning Perl by Randall Schwartz]. For the syntax to create HTML form elements (checkboxes, text fields, etc.), see the JavaScript Quick Reference
arrays
An array is simply a list. A simple array in Perl is designated as @myarray. An associative array (%myasscotiativearray) has a key and value for each item.
@myarray = (item1,item2,item3)
$item = $myarray[2] - returns item 3 from the array
%myassociativearray = (key1,value1,key2,value2,key3,value3)
$item = $myassociativearray{key1} - returns value1
cgi-lib.pl
A frequently used Perl library which simplifies the process of creating CGI scripts, especially through the use of the ReadParse sub-routine, which breaks form elements down into their individual values.
&ReadParse; - calls that sub-routine from the cgi-lib.pl
$recipient = $in{'recipient'}; - puts the data from the form field recipient into a perl variable with the same name
comments
Perl: Comments in Perl are introduced with a pound sign (#) at the beginning of the line.
# This is a comment line.
HTML: Comments in HTML are introduced with a an exclamation mark (!) and end with a double dash(--). They are sometimes used as markers for CGI scripts, in order to find locations in files to write to; may contain counters which are updated.
<!--startcomments--> - placeholder for CGI script to begin writing
<!--number0--> - counter to be updated by CGI
form
HTML tag for creating Web form. There must be a opening and closing form tag. Forms may be given names. An "action" handler is added to the opening form tag if a CGI script will be processing the form (i.e. action= "http://www.mysite.com/cgi-bin/myscript.cgi").
<form name=myform action= "http://www.mysite.com/cgi-bin/myscript.cgi">
(All the form elements defined here)
</form>
hidden field
HTML tag for creating a form field not displayed on the page but which passes values on to a CGI script or to JavaScript functions.
<input type=hidden name="use_mail" value="no">
if...then
Control structure for creating branching of actions depending on user input or choice. The condition for determining the result must follow if and be enclosed in parentheses. Action to be carried out is defined in curly brackets. Multiple if statements may follow the initial if through the use of else if. If may also be used by itself. Note that the test for equality in Perl is a double equal sign (==) for numerical values and "eq" for text; does not equal is != for numbers, "ne" for text. With functions which return a value of true or false, no equalizing symbol (==) is needed. Multiple conditions may follow the initial condition (all in the same parentheses) if separated by a double pipe (||) which is equivalent to "or"; to signify "and" (i.e. more than one condition must be fullfilled), use a double ampersand (&&).
if ($myvariable eq "right input") {
print "Right!";
} else {
print "Wrong!";
}
regular expressions
Perl excels at text manipulation. The implementation of regular expressions in Perl enables sophisticated search and replace. Here are some of the key elements of Perl's regular expressions:
. matches an arbitrary character
(...) groups a series of pattern elements to a single element
^ matches the beginning of the target
+ matches the preceding patern element one or more times
? matches zero or one times
* matches zero or more times
\ escapes any special meaning of the following chartacter and has special meaning for some characters:
\w matches letter or number
\s matches white space (\S matches non-whitespace)
\d maches any number (\D matches non-numeric)
\1..\9 refer to matched sub-expressions, grouped with (), inside the match.
require
Imports functionality and variables from a perl library; "use" is used to refrerence a perl module (.pm file)
search and replace
Searching and replacing is done with "s/pattern/replacement/"
$searchstring = "In the white room";
local($searchstring)=@_;
$searchstring =~ s/white/black/;
return $searchstring; - returns "In the black room"
strings
Text strings are enclosed in double quotes.
$newvariable = "This is a text string"
sub-routines
A block of code to be executed. To begin definition, use "sub", to evoke a sub-routine, use & as a prefix to the sub-routine's name (&myRoutine; would run the routine below).
sub myRoutine {
statements
}
variables
Defined in Perl with the $ prefix. Variables can be interpolated through being enclosed in quotes. (i.e. renders value of a valuable).
$myvariable = "This is some text"
ITA Resources for CGI
CGI => Index / Presentation / Code / Guide // AIA Portal
Bob Godwin-Jones