Perl is defined by the familiar ASCII character set, with white space separating the tokens that could be confused as a single token. Comments are inserted by placing the # character at the beginning of the line. That is, they are exactly the same as for Shell scripts (# works the same way as // in C++). You can also comment out sections of your file by placing an = at the beginning of the line, and ending the comment with a =cut. This is usually used for documentation, and the Perl distribution provides some utilities that will convert this documentation to LaTeX, HTML, or manpage documents.

There are three ways to run a Perl script in the Unix environment.

  1. Entering the script directly at the shell prompt.
  2. Saving the Perl script in a file, then providing the filename as an argument to the Perl interpreter.
  3. Saving the Perl script in a file with #! ("shebang") notation, making the file executable, and typing the filename to execute the script.

Entering a script at the shell prompt

Perl scripts can be entered directly at the shell prompt with the following syntax:

% perl -e 'script'

This runs the Perl interpreter with the script as an argument. The -e option prevents the Perl interpreter from looking for a file named 'script', and from complaining when it is unable to find one. Also, the single quotes surrounding the script are crucial, since otherwise the shell may try to interpret parts of your Perl script!

For instance, the following command prints the string "Hello World!" to the screen followed by a newline character:

% perl -e 'print "Hello World!\n";'

Although this method is very fast for executing short "run-once" type scripts, it can become extremely tedious when a script is used more frequently.

Providing a Filename Argument to the Perl Interpreter

Suppose that you are so fond of the script that prints "Hello World!" to the screen that you run it at least ten times a day. You will quickly find that you are tired of typing the 34 characters (including the carriage return) required to execute your script. Thankfully, the Perl interpreter is just as happy to execute a script saved in a file as it is to execute a script provided on the command line. For instance, consider the following example:

% cat > hi.pl
print "Hello World!\n";
% perl hi.pl
Hello World!

We stored our script in the file hi.pl (the .pl extension is a naming convention, similar to the .cpp extensions that you are probably familiar with for C++ program files). The Perl interpreter then read the script from the file, and executed it.

Making an Executable Interpreted File using "Shebang Notation"

Suppose now that we would like people to be able to run the script without knowing if it is a Perl script, a shell script, or an executable file obtained by compiling a C++ program. We need to avoid having to specify the name of the Perl interpreter on the command line. This can be done by including all of the necessary information inside the file. If we set the file's execute permissions, then all we will have to type to execute the script is the filename. The following example illustrates how this is done:

%  cat > hi.pl
#!/cs/local/bin/perl
print "Hello World!\n";
%  chmod +x hi.pl
%  ./hi.pl
Hello World!

The first line in file hi.pl is what is known as "shebang notation". This is because the # character is often referred to as sharp and the ! character is referred to as bang. Immediately following these two characters is the path of the Perl interpreter used to execute the script. Note that this path may be different outside of the Undergraduate servers. For this notation to work, this line must be the first line of the file. Recall that the command chmod +x hi.pl sets the user execute permission bit of the hi.pl file. Now all that is left to do is type the name of the script at the command line to run the script. The process of executing a Perl script cannot be simplified further.

Debugging, and other Perl Interpreter Options

There are various interpreter options that can be specified when running a Perl script. This is tantamount to the various compiler options that can be specified when compiling a C++ program.

If something goes wrong, you may get error messages or you may get nothing. You should always run the program with all warnings enabled using the command:

% perl -w filename
at the prompt. This will display warnings and other messages before the interpreter attempts to execute the program. We also strongly recommend that you insert the command
use strict;
as the very first line in every Perl script you write. Perl will check for potential mistakes, thereby helping you to avoid common Perl pitfalls.

To run the program with a debugger you can use the command:

% perl -d filename
or, alternatively,
% ddd filename.pl
and as long as filename ends with .pl, ddd will figure out that it is a Perl script that you want to debug.

If you would like to print out information regarding the version of Perl that you are currently running, you can use the -v option. Hence the command:

% perl -v
will display all of the version information.

Perhaps the most important option is -h. This displays a summary of all of the Perl interpreter options. The command:

% perl -h
will display the options available. Type this command at the prompt now and browse through the various options.

For more information about the execution of Perl scripts, you can consult the "Execution" section of the Perl man pages by typing man perlrun at the prompt.