Date Published: 1999-11-01
by D. Jasmine Merced
TNS Group, Inc.
New Perl Elements
Moving forward, we will assume that you are comfortable with the above terms and will continue onto how
these various elements work together to form a working program. To build a working cgi program in Perl,
there are other elements that we have not discussed, such as the "path to Perl", comments, conditions and
loops, operators and functions, and "Content-type" lines. We will address each of these in turn.
Path to Perl
Because Perl is considered an interpreted language (that is, a program that is read and executed
line-by-line), programs that run in Perl require an interpreter. An interpreter is what reads the line
of code and executes it. Called the "Perl interpreter" or the "Perl compiler", each program needs to let the server it runs on
know where the Perl interpreter is located. So when a program is run, it tells the server "Hey, the
interpreter is here… use that to run me." As with everything else, the path to Perl requires a very
specific format. It must be located on the first line of the program, and must be in the following format:
The #! (sometimes called a hashbang) tells Perl that the text
that follows is the location of the Perl interpreter. The path to Perl must be correct for your server
for the program to run. If it is not correct, you'll receive that lovely "Internal Server Error". If
you're not sure what your path to Perl is, your friendly neighborhood server administrator can tell you.
It's important to note that the exclamation point (!) is
immediately after the pound sign (#), or the Perl interpreter
will consider this line a comment (discussed below) rather than the path to Perl.
Often used by programmers to keep their code understandable (or to remember what an esoteric line
does), comments can be used anywhere in a program. Comments require a pound symbol
(#) before the comment. Anything after the
# will be ignored by the Perl interpreter. Consider:
In both of the above lines, the Perl interpreter will ignore "This is a comment." on both
lines. When reading through any program you may download, it's useful to look for any comments to
give you a better understand of what's being done where.
Conditions and Loops
Don't get scared - conditions and loops are very easy to understand. A condition simply tells the program
to perform and action if something is either true or not true.
For example, let's say that you have a feedback form on your web site and that the feedback form has
a checkbox that your visitor would check if they wanted to be added to your mailing list. So, the
condition is "if the checkbox is checked, then add the email address to the mailing list". In a
Perl program, this could look like:
This sample assumes that your form uses "yes" as the checked value.
The if ($checkbox eq "yes") is the condition - it asks the
program to see if your form's checkbox is checked.
If it is true, then it calls the &add_to_mailing_list
subroutine, which will then perform the necessary functions to add the email address to the list of
mailing list addresses.
If it's not true, then it won't do anything at all. But let's say that you want to give a confirmation
to your user that their address has not been added to a mailing list?
This is easily accomplished by adding an else statement to
the previous example.
Notice that nothing in the program has changed - only added. The
else statement allows you to tell the program to "do this if
this is true, otherwise, do this instead".
There's one more of this type of condition statement which allows multiple
else's. What if you wanted to offer the people on your
mailing list the option of receiving either html, rich-text, or plain text emails (using form radio buttons)? Using the basic if … else statement, you could do 2, but the 3 would be lost.
Perl allows you to set multiple conditions very easily. Consider the following code:
The above will tell Perl which mailing list to add your visitor to based on which of the radio
buttons were checked. This set of conditions is commonly referred to as
if… then… else statements. It checks the first condition, and
if true, executes the code. If not true, then check the second condition, etc.
While statements, on the other hand, continue performing an action (loops or iterates) while something
remains true. Because this may be difficult to explain without introducing several more advanced functions,
let's consider a very simple example.
Basically, the above code will open a file, and while there is a line in the file, it will print out
each line of the file. Then, it will close the file.
If the file isn't present, the program will terminate and give you a reason why it was terminated (the
purpose for the || die $!).
Operators and Functions
In its simplest definition operators manipulate data. Operators can either result in a new piece of data,
or modify the original piece of data. In this section, we'll discuss some of the most commonly used
A scalar is the term coined for data and is frequently used synonymously. Scalars can either be textual
or numerical, and there are operators specific to each.
Operators for numbers are typically mathematical in nature. The operators include + for addition, - for
subtraction, * for multiplication and / for division. Let's take a quick look at an example of numerical
operators in action.
$age = 1999 - 1969;
Of course, $age will equal 30. You can also perform
mathematical functions on other numeric variables. For example:
Same result. $age = 30.
Textual values (also called strings) can be concatenated, or put together using a period. For example:
$cool = "Perl " . "is" . "cool";
In this example, $cool equals
Perl is cool. But this probably doesn't look very useful.
Consider the simple mailing list example in Conditions and Loops above. Suppose you wanted to the
"You have chosen not to be added to our mailing list" line to include the email address that they didn't
want subscribed (we'll assume the email address provided in the feedback form is stored in a variable named
There are also simple methods to compare numeric or textual values. These "logical comparison operators"
include the standard greater than, less than, and equal to. Below is a list of these logical comparison
|Less than or Equal to
|Greater than or Equal to
Let's say you have a mailing list program where people can subscribe themselves and you want to
target your next email to them. Perhaps people between the age of 18 and 30 - the right target market
for a movie you're promoting. Here's a sample of how a Perl program would isolate these people.
It's useful to note here that Perl can treat numbers as though they were text in addition to
treating them as numbers. This is why there are separate logical comparison operators for both text
and numbers. Just because you have a number in a scalar (for example, an email address like
email@example.com) doesn't mean that you can perform mathematical functions on it.
Functions, on the other hand, are reusable blocks of code that return a value. There are many, many
functions embedded in perl, so we will mention chop and chomp.
It is extremely important to know the difference, especially if your web server is using a version less
than perl 5.0.
chop removed the last character of a string. chomp removes the newline character of a string. A
newline character can be considered the "hard return" character, such as when you hit an "enter" at the
end of the paragraph. chomp removed the last character indiscriminately. A newline character in perl is
represented as \n (a backslash and the letter n). chomp removes this newline "character(s)". Let's
take a look at a couple of examples.
$line = "Test";
chomp wouldn't serve any purpose here, because "Test" doesn't have a newline character. However, chop
would remove the last "t", making $line equal "Tes".
$line = "Test
In this case, chomp would make "Test
equal "Test". chop would also remove the newline "character" and make "Test
While there are many, many other operators and functions, we consider them "advanced" operators, and
will not be covered in this "intro" series. Be on the lookout for intermediate/advanced articles in the
When using perl to develop cgi programs, you need to tell the server and browser how data will be
outputted. In a cgi/perl program, you usually would output text in either "plain text" or html format.
This Content-type line must be the first item sent to the browser, or the program will return an Internal
Conclusion Home *
Previous: Review *
Next: Putting it all Together
This line can appear as follows:
print "Content-type: text/html\n\n";
This tells the browser that the text that follows is to be displayed as an html page. Notice the two
\n\n that is immediately after the html. These two newline
characters (remember that a newline character is \n) are
D. Jasmine Merced is a partner in Tintagel
Net Solutions Group, Inc. and the administrator of The Perl Archive. She also serves as a Director of
the World Organization of Webmasters.