Elements of Programming with Perl

By: Rachel McGregor Rawlings

Author: Andrew L. Johnson
Publisher: Manning Publications
Rating: 3.5 camels out of five

Perl, "they" say, is a poor choice for a first programming language. Elements of Programming with Perl, by Andrew Johnson, attempts to put a lid on such nonsense once and for all by providing the well-written equivalent of a first-year computer science text with all concepts and examples explained in Perl.

Johnson, editor of the Winnipeg Perl Mongers' newsletter, avoids a lot of the dumb mistakes that can be found in many Perl books that come from outside the solid Perl-using community. However, he does spend a lot of the book promoting non-canonical perl -- for both good and ill.

Johnson is a fan and practitioner of literate programming, which some may think completely incompatible with Perl. (Those who hold such opinions would do well to keep repeating TMTOWTDI under their breath while reading this book.) The other point of Elements is to serve a first text in programming, using Perl. Under such burdens, Johnson often produces sample code that looks more like Pascal than the Perl you may be accustomed to. But for the newbie, this can a good thing. Once the basic concepts are down, laziness and hubris may be added later.

Where Johnson does go wrong, in his excessive literacy and generality, is giving short shrift to some of Perl's best features, notably $_. Not simply on first reference, but throughout the book, Johnson uses constructions like

foreach my $var (@list) {
	print "$var\n" if $var > 5;

}
This ill serves the new Perl programmer who later has to look at another's code and wonder what all those funny one-character variable names are. Nor are there any mentions of pack and unpack, databases, or process control. Admittedly, these won't be found in a first-semester computing text, either, but those aren't typically written with Perl. Elements is not as unix-centric as the O'Reilly books; to be happy or annoyed is left as an exercise for the student. (He does give MacPerl its proper respect.)

Where Johnson excels is in explaining concepts that, when handled too tersely in other documentation, lead to FAQs. Those who read his explaination of arrays will be highly unlikely to run around wondering why they can't make a list of lists by inserting one into the element of another, rather than using a reference.

Whenever you picture an array, you should keep the list representation in mind. That way you won't find it surprising that you cannot store an array as an element of a list:
@foo = (1, 2, 'three');
@bar = ('four', 5, 6);
@new = (0, @foo, @bar, 7);

	is the same as:

@new = (0, (1, 2, 'three'), ('four', 5, 6), 7);

	which resolves to:

@new = (0, 1, 2, 'three', 'four', 5, 6, 7);
Some may find that a touch pedantic, but to the newbie it's pure gold that will keep them from asking dumb questions on IRC.

Johnson's discussion of regular expressions is first-rate. After a gentle introduction using the match operator in earlier chapters, Johnson devotes two whole chapters to the regex engine and the match and substitution operators. He jumps into a clear explanation of greed right off, again staving off a boatload of FAQs. He also provides a nice explanation (with plenty of $_) of map() and the Schwartzian Transform, which has become the ObCoolThing to write about, and should inspire some JAPHing in the book's readership. Let's hope the readers also study the very good chapters on modules and debugging.

One place many people who decide to learn Perl to write CGI scripts go wrong is that they start reading about the language and assume that makes them a programmer. For them, the chapter on debugging may be worth the price of admission ($34.95 US). Try the following example, look for the bug, and compare your results to Johnson's explanation on the following page. Here's the code:

sub max_length {
    my @list = @_;
    my $max = length (shift @list);

    for (my $i = 1; $i < @list; $i++) {
	my $length = length($list[$i] );
	$max = $length if $max < $length;
    }

    return $max;

}
Now here's the lesson:
The programmer thinks there are two ways to avoid an unnecessary comparison of the length of the first element in the list with itself: 1) shift the first element and get its length, then compare to the rest of the array, or 2) get the length of the first element (without shifting it off) and compare the rest of the array starting at the second element (index 1). So, perhaps the programmer wasn't paying enough attention when writing this relatively simple routine and accidentally implemented both ideas -- thus skipping the comparison with the second element. The routine appeared to work in a few small cases that never happened to have the longest element in the second position.
This may also be the time to note that Johnson forgets to tout the Benchmark module, but let that wait until his students are ready for their continuing education in efficiency. His job is to provide them with a clue, and he does it well.

Compared to the "Teach yourself in X amount of time" books, Elements is a real winner. Though there's material in here that must be unlearned by anyone who wants to go on to advanced Perl, the misdirections are mostly harmless, and do not lead into dangerous territory or outright BS. Elementsis not a replacement for the Llama (Learning Perl, by Randal Schwartz and Tom Christiansen). It is a very good introduction to Perl -- and programming -- for the neophyte who has never taken a programming course, or the webmaster who never learned the lessons that would be taught in one. It makes a great companion to the Llama for those who need more thorough explanations, or just like a little theory with their tools.