The details of how to run Perl vary depending on your operating system. The instructions that come with your Perl installation contain all you need to know. I'll give short summaries here, just enough to get you started.
On
Unix or Linux, you usually run Perl programs from the command line.
If you're in the same directory as the program, you can run a Perl program in a
file called this_program
by typing perl
this_program
. If you're not in the same
directory, you may have to give the
pathname of the program, for example:
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, because different machines may have installed Perl in different directories. On my computer, I use the following as the first line of my Perl programs:
#!/usr/bin/perl
You can type which
perl
to find the pathname where Perl is
installed on your system.
You can make the program executable using the chmod program: for instance, you can type:
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
. If the program is in a directory
that's included in your
$PATH
or $path
variable, you can type this_program
.[2]
If your Perl program doesn't run, the error messages you get from the shell in the command window may be 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. Watch for that, especially when running programs from
CPAN (see Appendix A), which
may have different pathnames for Perl embedded in their first lines. Also, if
you type my_program
, you may get this error
message:
bash: my_program: command not found
which means that the operating system can't find the program. But it's there
in your current directory! The problem is probably that your $PATH
or $path
variable doesn't include the current directory, and so 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
.
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—see the discussion in
Chapter 6).
The new Mac OS X is a Unix system on which you have the option of running Perl programs from the command line as described earlier for Unix and Linux systems.
On Windows
systems, it's usual to associate the filename extension .pl with Perl programs. This is done as part of the Perl
installation process, which modifies the registry settings to include this file
association. You can then launch this_program.pl by typing this_program
in an MS-DOS command window or by typing perl
this_program.pl
. Windows has a PATH variable
specifying folders in which the system looks for programs, and 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, for instance
perl
c:windowsdesktopmy_program.pl.