Running Perl Programs
April 26, 1999
A Perl program, as we'll soon see, is a text file containing
Perl code. There are several ways to launch a Perl program,
depending on your needs and the operating system in which you
develop.
Typically, you'll see that many Perl programs found on the
net or in tutorials begin with the line:
#!/usr/bin/perl
The above line is actually intended for UNIX systems -- other
operating systems will simply ignore it. However, because
UNIX is typically considered the de facto platform for Perl,
many Perl programs include this line. It tells UNIX where to
find the Perl program; the path /usr/bin/perl is a
common location for UNIX-based Perl installations. If you
are working in a UNIX environment, it is important that this
first line point to the correct location for Perl on your
particular system; alternatives may look like, for example:
#!/usr/local/bin/perl
#!/usr/bin/perl5
In either UNIX or Windows, if the perl program is in your
search path you can also run a Perl program simply with
the command line:
perl programName
Windows users might also use associations to run Perl scripts.
Windows associations assign a particular program to run when
a filename with a particular extension is launched. Thus,
you can associate the perl executable with filenames ending,
for example, in .plx or .pl. ActiveState's Perl
installation
will setup such an association for you. You can then launch
Perl programs by simply double-clicking them from the desktop.
Keep in mind that for Web developers, many Perl programs will
be launched in the context of CGI. We'll talk more about
CGI later, but for now you should understand that using Perl
programs for CGI involves a combination of the Web server's
configuration and the above methods of launching Perl programs.
Show me the Perl: Greetings
A Perl script can be a very simple thing. The following
program does only one thing: outputs the message "Welcome
to Currency Central".
#!/usr/bin/perl
print "Welcome to Currency Central\n";
What could be simpler? The only bits in need of explanation
here are two: first, note the \n. The backward slash (\)
indicates an escape sequence, which refers to a special
character that cannot easily be typed on the keyboard. The
\n escape sequence represents a newline, causing there
to be a linefeed in the output following the word
"Central".
Also notice the semicolon which represents the end of a
Perl statement. Not all Perl statements require semicolons
at the end, but it is good practice, since omitting a semicolon
where one is required will definitely result in a program error.
It is always a good idea to add comments to your program code,
which make it easy to remember what a particular line does.
Sometimes this is not obvious by simply reading the line of
code. Comments in Perl begin with a pound sign (#), and
continue to the end of that line.
#!/usr/bin/perl
#Output welcome message
print "Welcome to Currency Central\n";
Simple enough, but this isn't exactly an impressive display
of Perl power. Programming languages derive much of their
power from the use of variables. Almost every developer has
encountered variables in one scenario or another -- a variable
is simply a "container" or label which possesses a
certain value. We begin our study of Perl in earnest with a
look at variables in Perl.
Getting Perl
The Perl You Need to Know
Perl Variables: Scalars
|