First Time Linux


Perl is a powerful scripting language which is popular for certain types of web programming and for ad-hoc file processing tasks. It's extremely good at matching, replacing and processing text using regular expressions, making it ideal for automating file conversion.

The programs, which are also known as scripts, are read by the perl interpreter and compiled (invisibly) and executed in one step - in this way it seems much more like a shell script or a javascript than a compiled language like C++ or java where you first generate the compiled product and then execute it. However it is compiled, behind the scenes, and this allows it to make syntax checks before it starts to execute. The ease and terseness of the language make it a popular choice for small, "quick-and-dirty" tasks, but its power makes it possible to develop full applications as well.

There are no competing compilers or runtime environments for Perl, there is only the free one from the Perl Foundation. It is however available for free for a bewildering variety of operating systems all the way from Acorn to z/OS. The number of additional modules available from CPAN to extend the functionality is even more bewildering.

Getting and running

Getting perl is as simple as searching for "perl" in the package manager and pressing "install". Hey presto, perl. There are lots of other packages too, for interfacing with other modules (apache, Tk, XML) but for now just the basics will do. Here on Mandriva LE2005 it's listed as perl-5.8.6-6.1.102mdk. Now let's run it and see what version it is:

$ perl -v
This is perl, v5.8.6 built for i386-linux

(plus some licensing text). We're good to go.

First, let's use the console to run our first Perl script. Typing perl tells it to accept perl commands until <Ctrl-D> is pressed to terminate:

$ perl
print "hello from perl\n";
hello from perl

It should be obvious what's going on here, the print statement does what it should and the \n represents a newline character, like in C++ or java. This method of entering the commands into the interpreter (rather than saving as a file) is not recommended, because you can't then easily edit the script or run it again. But at least now you know why if you only type perl then it just waits for more input.

Running a saved script

Now let's save our commands in a file, so we can run it any time we like. Use an editor (doesn't matter whether it's a graphical one like Kate or a console one like vi, use whatever you prefer) and create a new file called

# String manipulation
my $perlis = "amazing";
$perlis =~ s/am/cr/;
$perlis =~ s/ing/y/;
print "Perl is $perlis\n";

# Looping over an array
my @evens = (2, 4, 6, 8, 10);
foreach (@evens) {
        print "$_ times $_ is ", $_*$_, "\n";

After saving this file, we can give it straight to the interpreter:

$ perl
Perl is crazy
2 times 2 is 4
4 times 4 is 16
6 times 6 is 36
8 times 8 is 64
10 times 10 is 100

In this case we define a variable called $perlis (the $ indicates that it's a scalar variable, but we haven't said whether it should be a string or an integer or whatever). Then we perform a couple of regexp manipulations on it and print it to the screen. The second part defines an array variable called @evens and loops over it. The 'magic' variable $_ is the so-called 'default variable' which pops up everywhere you don't explicitly name a variable to use.

The perl command has several optional parameters, the most useful of which are -p, -e and -w. See the man page or the documentation for their meanings.

Making the file executable

In a similar way to shell scripts, we can also make our executable and specify that it should be executed by the perl interpreter. That way we don't have to type perl to run it. Personally I'm not fond of this technique but I mention it for completeness.

Firstly, edit the file to include the following line as the first line of the file:

#!/usr/bin/env perl

Next we need to give ourselves execute permission for this file:

$ chmod u+x

and then we can execute it directly:

$ ./

More info

The first port of call should be the definitive which has a wealth of information including a raft of documentation. A good place to start is the perlintro. These documents are also available from their man pages at for example man perl and man perlintro. There are more FAQs at, and also see Wikipedia's Perl page.