LIS 525 - Perl

Perl (Practical Extraction and Report Language) is the language most commonly used in developing CGI (Common Gateway Interface) interactions.

Perl is often already installed on Linux/Unix systems. You can test this with the command

perl -v
For example, on panther.uwo.ca, this command produces the response
This is perl, version 5.004_04 built for sun4-solaris

Copyright 1987-1997, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5.0 source kit.

Comment lines in Perl begin with #. A special variety of comment line beginning with #! can be used to point to the location of the Perl program on the server. This would typically be done for a CGI script. The use of CGI scripts on Web sites on the University's servers is, of course, restricted.

A statement in Perl usually ends with a semicolon (;); omitting a semicolon is a frequent cause of errors in Perl scripts.

Numeric (scalar) and string variables begin with a dollar sign ($). They may be inserted within the double quotes used to mark literal strings (for example print "$USTotal American dollars"), in which case their values are automatically inserted as part of the string.

The period (.) is used for string concatenation.

The double equals sign (==) is used for arithmetic comparisons, and eq is used for string comparisons, while the single equals sign (=) is used for assignment.

The name of a list is prefixed with @. When referring to individual list items, however, the prefix is $. The specific item in the list is identified in square brackets. The first item in a list is item zero; for example, $mylist[0].

Lists of key-value pairs, which can be used for tasks like frequency counting and are referred to as "hashes", are characterized by the prefix %. Individual values can be identified by means of the key in curly brackets; for example, $freq{$word}.

Various functions are available.

There are if and unless conditions, and while, for, and foreach loops. For each of these, blocks of statements are enclosed in curly brackets, and conditions in parentheses. The for keyword is followed by a list of three expressions separated by semicolons and with the three surrounded by parentheses, indicating the initialization, the condition for continuing, and the action to take at the end of each iteration; for example, for ($j=1; $j<=10; $j++) (which means to repeat for each value of j from 1 to 10). The foreach keyword is followed by a variable name and then a list name in parentheses; for example, foreach $listitem (@mylist).

An example of a Perl CGI script is the following, which increments a counter in the file counter.txt and delivers an HTML page showing the new value of the counter:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
#
$filepath = "/home/craven/counter.txt";
open(NUMBER,"$filepath");
$ctr = <NUMBER>;
close(NUMBER);
$ctr++;
print <<STOP;
<html>
<body>
STOP
print "The counter is now $ctr.\n";
open(NUMBER,">$filepath");
print NUMBER "$ctr";
close(NUMBER);
print <<STOP;
</body>
</html>
STOP
exit(0);
The location of the Perl program and of the file counter.txt will, of course, need to be modified to fit the particular server configuration.

For More Information

For a quick introduction, see

For slightly more detail, see

For links to more information on Perl, see

Some free Perl scripts are available at You can download a free version of Perl for Windows from
Home

Last updated October 19, 2007.
This page maintained by Prof. Tim Craven
E-mail (text/plain only): craven@uwo.ca
Faculty of Information and Media Studies
University of Western Ontario,
London, Ontario
Canada, N6A 5B7