You Make Me Want to Perl
Posted by postfuturist on 2009-10-29 00:17:59

I've always avoided Perl because of its reputation. And, well, it looks funny. The Perl folks like to golf, which leads to more confusion and incomprehensible code. But then I started picking up bits of information. The creator, Larry Wall, is linguist, not a computer scientist nut. There are constructions in the language that are unseen elsewhere but flow naturally making for a language which is beautiful at its core.

print "hello" if $happy;

It's a stronger, more elegant voice than the much more common:
if ($happy) { print "hello" };

Both are valid Perl. The first one requires less punctuation because the keyword sits between the two statements and there is therefore less ambiguity. It may seem strange to a programmer's eye but it is a cleaner syntax. They are also both valid English, and even in English the second construction requires a comma in the middle to disambiguate the meaning. The language feels like it was constructed by a linguist because it has linguistic constructions.

There are issues. The language is baroque in it's complexity. Regular expressions are a powerful, fundamental part of the language, and contribute to the "line noise" quality. The sigils, those symbol prefixes on variable names, are uncommon in modern languages and the use in Perl is confusing.

my @array = ("1", "2", "3"); # the @ signifies an array
my $number = $array[0]; # but you have to use the $ when extracting a scalar
my $arrayref = ["4","5","6"]; # and references to arrays are scalar
$#array -= 1; # what the hell is going on here? (shrinking the array)

That's just character, though. What's really going on is a lot of choices. Any programming paradigm fits Perl. It is fully "functional" so you can have anonymous functions, pass functions around like other data, and take advantage of closures.
sub outer {
my $a = 0;
return sub { return ++$a; };
}
my $inner = outer();
print $inner->(); # prints "1"
print $inner->(); # prints "2"

It's object oriented capabilities are deceptively simple but are, in fact, very open-ended and able to be used in any manner you see fit. Here is a simple example.
package Foo;

sub new {
my $class = shift;
my $obj = shift;
return bless $obj, $class;
}

sub bar {
my $self = shift;
print $self->{baz};
}

package main;

my $foo = Foo->new({baz => "hello"});
$foo->bar(); # prints "hello"


I will not explain in detail exactly what's happening on each line, but it will suffice to say that Foo is a class and $foo an instance of that class. At first, the system seemed unwieldy, but when I saw what Perl hackers have built on top of it, Moose, I began to understand how powerful this language truly is.

The real treasure of Perl is CPAN, a huge collection of Perl libraries where one finds likes of Moose and its extensions which are legion. This rabbit hole goes very deep indeed. Perl does very little to stop you from accomplishing your tasks, large and small, and provides the tools to create abstractions at many levels and with many paradigms. It's my latest obsession.



    Leave a comment: