Reading: Learning Perl

Posted by: Jarvis Cochrane 7 years, 10 months ago

Learning Perl
Randal L. Schwartz & Tom Phoenix
O'Reilly, 2001

Being me, when I could no longer elegantly dodge the necessity of learning Perl, I started with the no-holds-barred, no-obscure-detail-omitted, dense, complete and exhaustively definitive technical reference of Programming Perl. Now, the greater part of a year later, I've finally advanced to the much gentler and friendlier introductory tutorial Learning Perl.

The O'Reilly 'animal' books have become a kind of geek icon. I doubt there's a single programmer or systems or network administrator who doesn't have at least some of the O'Reilly menagerie on their bookshelf.

I've personally found the animal books to be so consistently helpful, accurate, and all-round useful that my rule of thumb when I'm learning something new is to always start with the O'Reilly book. I'm waiting with bated breath for O'Reilly to publish a Learning Russian!

The 'Llama book' (Learning Perl) and the 'Camel book' (Programming Perl) well deserve the esteem and affection the Perl community holds them in. These are simply the books to have if you're a Perl programmer.

The books are great, but the language itself still drives me nuts!

(Gentle readers who prefer not to be exposed to mild rants about cyber-linguistic esoterica are advised to head on over to Get Fuzzy at this point.)

It's part of the Perl mythology that Larry Wall's background as a linguist is one of the things that makes Perl such a uniquely great language. People writing about Perl are always very quick to draw parallels between Perl and natural languages, and frequently invoke Larry's linguistics background. Here are just a couple of random examples from the first chapters of Learning Perl:

In English, as in many other spoken languages, we're used to distinguishing singular and plural. As a computer language designed by a human linguist, Perl is similar.

Learning Perl, 2001 edition, page 19

[A] given expression may mean different things depending on where it appears. This is nothing new to you; it happens all the time in natural languages.

ibid., page 51

And an ironic personal favourite:

[A] big part of learning Perl is actually learning how Larry thinks.

ibid., page 52

I'm a heretic. I understand a little of 'how Larry thinks', and I think he's wrong. I think Perl would be a much better language if Larry thought a bit more like a programmer, and a lot less like a linguist.

Consider as a concrete example one of my least favourite bits of Perl grammar:

use warnings;
@cats = ('kess', 'pebbles', 'princess');
print $cats[0], "\n";
print @cats[0..1], "\n";

What I find frankly bizarre about the expression $cats[0] is that the @ sigil is a required and indivisible part of the variable name @cats, but when you are referring to only one element of the @cats array, the variable name changes to $cats. Which is irregular, confusing, and to the best of my knowledge without parallel in any other programming language.

But it's exactly what happens all the time in natural languages!

To refer to a single feline, we use 'cat'. To refer to a plurality of kitties we say 'cats'. But when we want to pick one particular cat out of the herd, we say 'the first cat', using the singular form of the noun even though there are many cats from which we are selecting the first.

(You could also say 'the first of the cats', but it's a bit awkward. In either case the whole clause is singular for the purposes of conjugation - the cat runs; the cats run; the first cat runs; the first of the cats runs. )

This is what's happening in the $cats[0] construct. Because we're referring to only one of the collected @cats, Larry prefers we use the singular form of the variable name. And when refer to more than one of the @cats (ie @cats[0..1]), we go back to using the plural form of the noun. Sorry. Variable.

It all makes perfect sense if you're an English speaking linguist!

But no other programming language does this. Simple syntax and regular grammar are almost univerally hailed as the cardinal virtues of programming languages, and there are even periodic attempts to simplify and regularise natural languages themselves! No lesser person than Ben Franklin thought that the founding of a new nation should be commemorated by simplifying English spelling and making it more fonetik.

Frankly I would have thought that a linguist, given the opportunity to create a new language from scratch, would know better than to introduce any irregularity that wasn't absolutely required.

It seems to me that Larry would love to give Perl all the marvellous subtlety and expressive power that natural languages have. He wants people to write poetry in Perl!

But people don't, in the main, use programming languages to write poetry. They use them to write exhaustively detailed prose descriptions of how data is to be structured and manipulated. Computer programs occupy much the same sitz en lieben as circuit diagrams, mechanical working drawings, or technical operating manuals. Their most important quality is not that they make aesthetic use of the language, but that they are as correct and unambiguous as possible.

A programming language with all the subtlety and expressive power of a natural language would presumably be as difficult to master as any natural language is. And it would presumably require a compiler roughly as smart as a human being. And such a programming language would presumably be every bit as innapropriate a tool for accurately describing data structures and algorithms as natural languages are.

Sorry Larry, I get it, I just don't think it's a good idea!

Update 2013-04-25: Edits.

Currently unrated