B.5 How to Run Perl Programs

The details of how to run Perl vary depending on the operating system of your computer. The instructions that come with the version of Perl you install on your computer contain all you need to know. I'll just give a short summary here that will point you in the right direction.

There is a part of the Perl documentation called perlrun. You can find it at http://www.perl.com/pub/doc/manual/html/pod/perlrun.html. It gives all the options of running Perl, especially on Unix and Linux; it's complete but not for the beginner.

B.5.1 Running Perl Programs on Unix or Linux

On Unix or Linux, you usually run Perl programs from the command line. You can run a Perl program in a file called this_program by typing:

 perl this_program

if you're in the same directory as the program. If you're not in the same directory, you may have to give the pathname of the program:

 perl /usr/local/bin/this_program

Usually, you set the first line of this_program to have the correct pathname for Perl on your system, since different machines may have installed Perl in different directories. On my computer, I use the following as the first line of my Perl programs:


You can type which perl to find the pathname where Perl is installed on your system.

You also usually make the program executable, using the chmod program:

chmod 755 this_program

If you've set the first line correctly and used chmod, you can just type the name of the Perl program to run it. So, if you're in the same directory as the program, you can type ./this_program or, if the program is in a directory that's included in your $PATH or $path variable, you can type this_program.[1]

[1] $PATH is the variable used for the shells sh, bash, and ksh; $path is the variable used for csh, tcsh, and so on.

If your Perl program won't run, the error messages you get from the shell in the command window may be a little confusing. For instance, the bash shell on my Linux system gives the error message:

bash: ./my_program: No such file or directory

in two cases: if there really is no program called my_program in the current directory, or if the first line of my_program has incorrectly given the location of Perl. So watch for that, especially when running programs from CPAN that may have different pathnames for Perl embedded in their first lines. Also, if you type my_program, you may get the error message:

bash: my_program: command not found

which means that the operating system can't find the program. But it's right there in your directory! The problem is probably that your $PATH or $path variable doesn't include the current directory, so that the system isn't even looking in the current directory for the program. In this case, change the $PATH or $path variable (depending on which shell you're using); or just type ./my_program instead of my_program.

B.5.2 Running Perl Programs on the Macintosh

On Macs, the recommended way to save Perl programs is as "droplets." The MacPerl documentation gives the simple instructions. Basically, you open the Perl program with the MacPerl application and then choose Save As and select the Type option Droplet.

You can drag and drop a file onto a droplet in order to use the file as input (via the @ARGV array).

The new Mac OS X is a Unix system, and on those systems, you have the option of running Perl programs from the command line as just described for Unix and Linux systems.

B.5.3 Running Perl Programs on Windows

On Windows computers, it's usual to associate the filename extension .pl with Perl programs. This is usually done as part of the installation process, which modifies the registry settings to include this file association. You can then launch this_program.pl by typing this_program or perl this_program.pl in an MS-DOS command window. Windows also has a PATH variable specifying folders in which the system looks for programs; this is modified by the Perl installation process to include the path to the folder for the Perl application, usually c:\perl. If you're trying to run a Perl program that isn't installed in a folder known to the PATH variable, you can type the complete pathname to the program, e.g., perl c:\windows\desktop\my_program.pl.