
-----------------------------------
wtd
Thu Dec 02, 2004 12:47 am

[Perl5-tut] Perl5 Primer
-----------------------------------
Goal

The goal of this tutorial is to introduce Perl5 to programmers who already have experience in another programming language.

What is Perl?

Depending on who you ask, Perl is the "Practical Extraction and Report Language", or the "Pathologically Eclectic Rubbish Lister".  Either way, it's a powerful programming language that's installed on pretty much every Unix or Unix-like system, and is available for just about any platform you can imagine.

Further, Perl, despite not having a large company like Sun or Microsoft strongly advocating it, has become incredibly popular.  Due to the amount of work already done in Perl, and the number of people continuing to work on it, it's also a well-known name, and has a lot of power on a resume.


A first program

use strict;
use warnings;

print "Hello, world!\n";

The first two lines force us to program strict Perl, which makes it easier to avoid errors, and enables warnings, to let us know when we're doing things which could be dangerous.  As in any language, warnings should not be ignored.

The third line simply prints "Hello, world!" to the screen.

Comments

Comments in Perl are anything following a #.

# this is a comment

Variables

Variables in Perl5 in strict mode are "declared" with the keyword "my".

my $hw = "Hello, world!\n";

print $hw;

There are only four types of variables in Perl, and they aren't what you're probably used to.

Scalars

Scalars are either numbers or strings.  The language goes back and forth easily between the two.

String concatenation with a number and a string...

print "foo" . 42;
# foo42

Similarly, two numbers...

print 1 . 5;
# 15

Mathematical operators work much the same way.

print "4" + 3;
# 7

print "3" + "12";
# 15

print 1 + "5";
# 6

To repeat a string, use the "x" operator rather than "*".

print "hello " x 3;
# hello hello hello 

Arrays

The second kind of variable is an array.  Array variables are denoted with a leading @, just as scalars are indicated by a $.  Each element in the array is a scalar.  An array can contain any number of elements.  Array indices start at zero.

A simple array:

my @foo = (42, "foo", 57, "hello world");

Accessing each inidividual elements in the array is done like so:

my $greeting = $foo[3];

The @ switches to a $ since the thing being accessed really is a scalar.

Inserting a value into an array is similarly simple, and you can insert at any index.  

my @foo;
$foo[10000] = 42;

Getting the length of an array is a matter of:

my $length = scalar @foo;

Hashes

The third type of variable in Perl is the hash, or associative array.  It's kind of like an array, but it uses a scalar as an index.  Also, hash variables are prefixed with %, and {} are used instead of square brackets when accessing elements.

my %bar = ("greeting" => "Hello, world");

print $bar{"greeting"};

$bar{"name"} = "Bob";

References

References are a fancy twist on scalars, and can go anywhere a scalar can.  Instead of a number, or string, though, they hold references to other variables (scalars, arrays, hashes).

The \ operator gets a reference.

my $str = "hello";
my $ref = \$str;

# or just

$ref = "world";

Of course, we can also get reference to arrays and hashes with \...

my @arr = (1, 4, 9);
my $arr_ref = \@arr;
my %hash = ("hello" => "world");
my $hash_ref = \%hash;

But, it's easier if we just directly create the reference using different brackets.

my $arr_ref = [1, 4, 9];
my $hash_ref = {"hello" => "world"};

Dereferencing a reference is easy.

my $scalar_ref = "hello";
my $scalar = ${$scalar_ref};

my $arr_ref = [1, 4, 9];
my @arr = @{$arr_ref};

my $hash_ref = {"hello" => "world"};
my %hash = %{$hash_ref};

For simple examples like these, we can elide the brackets.

my $scalar_ref = "hello";
my $scalar = $$scalar_ref;

my $arr_ref = [1, 4, 9];
my @arr = @$arr_ref;

my $hash_ref = {"hello" => "world"};
my %hash = %$hash_ref;

And if we want an element from an array or hash:

my $arr_ref = [1, 4, 9];
print $arr_ref->[2];

my $hash_ref = {"hello" => "world"};
print $hash_ref->{"hello"};

The real power of references is the ability to create arrays and hashes that contain other arrays or hashes.

my $person = {
   "name" => {
      "first" => "Bob",
      "last"  => "Smith"
   },
   "car" => {
      "make"  => "AMC",
      "model" => "Gremlin",
      "year"  => 1970
   }
};

print $person->{"car"}->{"make"};

String interpolation

Few little things are more useful in Perl than string interpolation.

I could write my "Hello, world" using the stringconcat operator:

my $hw = "Hello, world";
print $hw . "\n";

Or I could just put the variable into the string...

my $hw = "Hello, world";
print "$hw\n";

Comparisons

Comparing numbers is done with:

==, !=, >, =, 