Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Running Perl Scripts on Linux Systems

This section provides a tutorial example on how to run Perl scripts on Linux systems. To make a Perl script file executable, you need to add '#!/usr/bin/perl' to the beginning of the script.

There are many ways to run Perl scripts on Linux:

1. Run the "perl" command with the Perl script included in the command line. For example, enter the following command line in a command window:

>perl -e "print 'Hello world!';"
Hello world!

Another example:

>perl -e "for ($i=0; $i<10; $i++) {print \"$i\n\";}"
0
1
2
3
4
5
6
7
8
9

This is a cool way to run a Perl script quickly. But you can only run scripts that are small enough to fit into one command line.

Also note that double quote (") is used to put the entire script code as one command line parameter. Any double quote inside the program needs to be protected as (\").

2. Run the "perl" command with the Perl script supplied from the standard input steam. For example, enter "perl" in a command window. Then enter the script code followed by Control-Z, which is the End Of File (EOF) indicator:

>perl
$s=0;
for ($i=0; $i<10; $i++) {
   $s+=$i;
}
print "$s\n";
^Z
45

Obviously, you can enter a much longer script in this way. But the program is not save permanently.

3. Run the "perl" command with the Perl script supplied in a file. For example, enter the following Perl script in a file called hello.prg:

   print "Hello world!\n";

Then enter the following command in a command window:

>perl hello.prg
Hello world!

This is how you normally run Perl scripts.

4. Run Perl script files as commands. You can do this, only if you insert a special line at the beginning of your script file: #!/usr/bin/perl, and assign execution permission to the script file. This special line represents the Perl installation location on the file system.

For example, enter the following script in a file called hello.pl:

#!/usr/bin/perl
   print "Hello world!\n";

Then assign execution permision and enter the script file name to run it:

>chmod a+x hello.pl
>hello.pl
Hello world!

It works!

Sections in This Chapter

Perl Installation on Linux Systems

Running Perl Scripts on Linux Systems

Dr. Herong Yang, updated in 2008
Running Perl Scripts on Linux Systems