Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

Perl on Linux

This chapter describes:

  • How Perl is installed on Linux
  • How to run Perl programs on Linux.

Installing Perl on Linux

If you are running Linux, Perl is ready for you to use. I have Linux 2.0.30 running on my PC with a 386 processor. It was installed from a Linux distribution package called Darkstar. Perl was part of the distribution.

Here is how you can check your Perl install on your Linux system:

>which perl
/usr/bin/perl

>perl -v
This is perl, version 5.004_03

Copyright 1987-1997, Larry Wall

Perl may be copied only under the terms of either the Artistic License
or the GNU General Public License, which may be found in the Perl 5.0 
source kit.

>man perl
......

Running Perl on Linux

There are many ways to run Perl on Linux:

1. Run the "perl" command with the Perl program 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 program quickly. But you can only run programs that are small enough to fit into one command line.

Also note that double quote (") is used to put the entire program 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 program supplied from the standard input steam. For example, enter "perl" in a command window. Then enter the program source 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 program in this way. But the program is not save permanently.

3. Run the "perl" command with the Perl program supplied in a file. For example, enter the following program 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 programs.

4. Run Perl program files as commands. You can do this, only if you insert a special line at the beginning of your program file: #!/usr/bin/perl, and assign execution permission to the program file. For example, enter the following program in a file called hello.pl:

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

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

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

It works!

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - Perl on Linux