Perl Highlights
Perl is a very commonly used language for CGI scripts. It's
popular because it's a very powerful language and because
it is interpreted rather than compiled. Compiled languages,
like C, require that the program be run through a compiler,
which converts the English-like program statements into an
executable file that can't be read via a text editor. Interpreted
languages, on the other hand, are "compiled" on the fly so
there's no reason to generate a separate executable file.
That makes it easier for the programmer to make changes. It
also makes the programs very portable -- compiled programs
can only be run on the operating system that they were compiled
for. You can't compile a C program on a PC and then run it
on a UNIX server. With Perl, you just upload the text file,
make a few tweaks, and it will run on almost any operating
system.
In order to start installing pre-written CGI scripts on
your web site, you just need to have a high-level familiarity
with Perl. You need to know what it looks like, what lines
are safe to modify, and what lines to leave alone. You need
to know how to make changes in order to install the script
in your environment. You don't need to understand Perl to
the extent that every line of the script makes sense to you.
Editing perl files
Probably the first question most beginners ask is this --
how do I edit a perl script?
Since perl is just plain text, you should edit the files
with a text editor. A text editor is a simple program used
for typing and editing. The most basic text editors are Notepad
(PC) and Simple Text (Mac). Microsoft Word and similar programs
are not text editors. Notepad and/or Simple Text will serve
your purposes just fine, but there are other, fancier text
editors available as well. These programs have features such
as automatically formatting your perl files and showing line
numbers to aid in debugging. Here are a few of my favorites:
The first line
The first line of every UNIX Perl CGI script* contains the
path of the perl executable on the server. That is, where
is the perl program installed on your ISP? Open any perl script
in your favorite text editor and you will see something like
this:
#!/bin/perl
Where /bin/perl can be replaced with a different path.
You must know where perl is installed on your ISP in order
to configure this line properly. You can look in your ISP's
FAQS page for this information or email tech support. If you
have telnet access, you can find
out the path yourself.
The most common paths to the perl program are:
- /bin/perl
- /usr/bin/perl
- /usr/local/bin/perl
- /usr/sbin/perl
If you don't have this line correct, your script won't work
at all. Most likely you will get a 500
server error when you try and run the script from the web.
* Note: This line is ignored on NT systems
Comment lines
Any line that begins with pound sign (#) symbol in perl
is a "comment". That means the text following the
pound sign is not perl code, but is just extra text inserted
for clarity. Usually comments are used to help explain the
code to the reader.
Non-comment lines
All other lines in the file are considered to be perl code
and must be properly formatted in order for the script to
function.
Every non-comment line must end with a semicolon (;) in perl.
However, not all perl "lines" are on a literal line in the
text file. For example:
$mystring = "
hello
world
";
is only a single "line" in the strict sense, although it physically
spans several text lines. So just make sure you don't delete
any semicolons's that are in the file.
Strings
Strings are defined as a group of characters enclosed in
either quotation marks ("string") or single quotes
('string'). The quotation mark or single quote is called the
"delimiter" of the string: it defines the beginning and the
end.
Many times you will need to modify strings in order to install
a CGI script. For example, in the setup file for Links 2.0
from Gossamer Threads:
$db_script_path = "/home/ashley/www/links/cgi-bin/admin";
You would need to edit the string in order to put the correct
path for your admin script.
It is generally safe to edit strings as long as you don't
accidentally erase the delimiters. However, if you wanted
to use the delimiter symbol inside the string, you
must precede it with a back slash. For example:
$string = "my dog says \"hello\"";
would set the variable $string to [my dog says "hello"].
All of the backslashes can get pretty confusing, so there
is another way around this. You can replace the delimiter
marks to any symbol you like by using the notation
qq!string!
It is very common to see this syntax with either an exclamation
mark (!) or a tilde (~) as the delimiter symbol.
So, if you see a structure like this:
qq!
Some stuff here
!;
you can edit anything between the exclamation marks, but make
sure you don't accidentally erase the exclamation marks themselves!
You also cannot have any exclamation marks inside the text itself
unless they are preceded by a backslash.
qq!
I want and exclamation mark \! here
!;
Similarly, if you see
qq~ text here ~;
you must put a backslash before any tilde (~) characters within
the text.
Variables
Anything that starts with a dollar sign ($) is what's known
as a variable. You can either assign a variable to a number,
a string, or another variable. If you are assigning it to
a string (ie a group of letters and/or numbers), you must
enclose the string in single or double quotes (or another
delimiter as described above). You must put a semicolon at
the end of the line.
Arrays
Anything that starts with an ampersand (@) is what's known
as an array. That is, a *list* of numbers, variables, or strings.
Your list must begin with an open parenthesis [(] and end
with a close parenthesis [)]. Again, there must be a semicolon
at the end of the line. Within the parentheses, you can have
a series of numbers, strings, or other variables, each separated
by a comma. Again, strings must be enclosed in delimiters.
From the Web
Store ES setup file:
@sc_db_index_for_detail = (
$db{"item"},
$db{"name"},
$db{"description2"},
$db{"image2"},
$db{"price"},
$db{"weight"},
$db{"shipexempt"},
$db{"taxexempt"},
$db{"size"},
$db{"color"},
$db{"options"}
);
Due to the special meaning of the ampersand symbol in perl
to designate arrays, if you write an email address anywhere
in a perl file, you must write it like this: ashley\@enscript.com
with a backslash before the ampersand. This is a very common
mistake!
Related tutorials
UNIX directory structure
Useful UNIX tasks
Basic UNIX commands
Using telnet
|