Developer

Perl 101

Perl is commonly referred to as 'duct tape of the Internet'. Get Perl into your toolbox by following this introduction by Nick Gibson.

Perl 101

Perl has been the most commonly used scripting language for the past ten years, earning it the reputation of being the "language behind the Web", if you've never got the chance to see what all the fuss is about, this guide can get you started. Perl is a high-level interpreted programming language, building upon C, as well as shell scripting languages and tools such as awk and sed. While designed, and still most commonly used, as a replacement for those scripting languages, the large variety of community written modules has increased Perl's reach, making it a serious choice for tool development in almost any area.

Why Perl?

  • It's Free: With it's open source license, trying out Perl is completely free, and should you decide to keep using it, it won't cost you or your business a cent.
  • It's Fast: While slower than an assembler compiled language like C, Perl tends to run most programs faster than similiar interpreted language such as Python or Ruby, and much faster than equivalent shell or awk scripts.
  • Naturally interacts with the shell and UNIX: Perl is built to work with the operating system, opening file handles, devices, redirecting to pipes or sockets is easy to work with on Unix. With Windows things can get a little trickier.
  • Regular Expressions: Regular expressions are a programmers greatest tool when working with text. In Perl, rather than being included as part of a library, regular expressions are a basic part of the language, and no other language can match the maturity or simplicity of use of Perl's regular expression syntax.
  • CPAN - The Comprehensive Perl Archive Network: CPAN is an archive of almost two thousand Perl modules, scripts, documentation and extensions. It's a central repository of Perl modules for virtually every conceivable purpose, meaning that when you need a module to do something in Perl you know just where to look.

Installation

Perl is so ubiquitous from a scripting standpoint that if you're in a UNIX environment, it's probably already being used by your operating system somewhere. It comes prepackaged with most Linux distributions, and with MacOSX 10.3 or greater. If you're running Windows, or for some other reason you don't have it, or you want to upgrade to the newest version, you can get the latest source (in C) at Perl's official Web site.

The Perl website only distributes the Perl interpreter in source format, but if you don't have easy access to a C compiler, or you just don't want to go through the hassle of compiling, then you can just download ActivePerl instead, a binary Perl distribution. Installation is simple, just download the installer (graphical for Windows and OSX, text based for UNIX) and follow the prompts. After you've installed you can test out your Perl install by running a simple command:

perl -le "print 'Hello World';"

If it echo's Hello World back to you then you're in business, otherwise, check that the install completed successfully and that the Perl install directory is in your path.

Perl Basics

Now that we've got everything up and running, it's time to demonstrate some simple Perl programs. Let's start with a determining high powers of numbers, this program prints x^y:

$x = 7;
$y = 12;

$result = 1;
while ($y > 0) {
    $result = $result * $x;
    $y—;
}

print $result;
print "\n";

When we run the program we get the following output:

% perl pow.pl
13841287201

In this program, a while loop is used to successively multiply $result by $x, each time the loop is run, the counter $y is decremented, so that it only runs $y times. In Perl, scalar variables, such as numbers or characters are identified by a dollar sign $ before their name, to differentiate them from other types. This program is not quite correct however, what happens if we want to compute a negative power, such as 7^-1. If we change y to -1 and run the program the loop will not be run and it will print 1. This is not correct, so we need to make a change:

$x = 7;
$y = -1;

$result = 1;
if ($y > 0) {
        while ($y > 0) {
                $result = $result * $x;
                $y—;
        }
} else {
        while ($y 

Here we use an if statement to branch the program, if y is positive, then we run the first loop, if it is negative, we run the second loop, which divides succesively and counts upwards instead, and if y is zero then it leaves result equalling one. If we run this program, we get the following output:

% perl pow.pl
0.142857142857143

Of course, Perl doesn't make you rewrite such a common function, it's built in: the ** operator. For example, the above function could just as simply be written:

$x = 7;
$y = -1;

print $x ** $y;
print "\n";

Finally

So there you have it, you've got Perl up and running and you're churning out programs. But the true advantage in using Perl lies in it's ability to handle command line inputs and text processing through the power of regular expressions. Stay tuned to Builder AU in the following weeks and we'll show you how.

Editor's Picks

Free Newsletters, In your Inbox