jmac reviews: Mastering Algorithms with Perl

Mastering Algorithms with Perl

by Jon Orwant, Jarkko Hietaniemi and John Macdonald

published by O'Reilly & Associates, Inc.

I knew right from the first few pages of O'Reilly's "wolf book", published in late 1999, that I'd be getting a lot from it, if it lived up to its own promises of merging the discipline of computer science algorithm study with the practicality of Perl programming. Allow me to admit that I am a member of the teeming mass of programmers who have found employment in a field that may have otherwise overlooked them if not for the recent and utterly unprecedented demand for hackers to feed the commercial Internet boom (well, I suppose anyone reading this review stands an excellent chance of being in the same situation, but anyway). I took only two computer science courses in college before deciding the pursuit of an English degree might be more fun, but graduated into a world far more thirsty for any sort of programming skill it could squeeze from me than whatever services I could offer as a copy editor or whatnot.

Here is where Perl comes in, for many of us; because this language, more than any other, serves as the glue (or, as many prefer metaphorizing it, the duct tape) that holds the World Wide Web together (for, as Neal Stephenson once wrote, all the Web's content, for all its apparent forms and flavors, is really nothing more than a collection of textfiles, and Perl can do magic on text like nothing else can), it's the first thing that employees of clueful Internet-based companies (such as the one for which I work) insist their programmers learn. However, in the rush of meeting the next assignment, it becomes far too easy to forsake elegance and efficiency for anything that works for the moment. And that, finally, is the gap which I found "Mastering Algorithms With Perl" attempting to fill. In my opinion, its appearance on bookshelves couldn't have waited a moment longer. I cringe to think of the overall quality of the code which currently supports the Web as we know it (browsing the source code of the offerings from any popular free script archive should convey this notion adequately to those with experienced eyes), and any effort to teach some manners this unwashed mob (of which I openly claim membership) I will welcome and read most gratefully.

The book's pacing, when absorbing it cover to cover as I did, seems a bit uneven at times, but once I worked up a certain attitude about its structure, I found myself more able to appreciate it. The first few chapters of of the volume clearly stand out as possessing the most valuable content, when read by themselves, for they reexamine some of the basic Perl data structures -- arrays and hashes -- in the light of generalized computer science theory. That array you've been using all this time? Well, it's actually what we can more generically call a queue. Or a stack, depending upon how you treat it. See this hash? Y'know, if you hold it up to the light just so, hey, it becomes a linked list. It's sort of a "Learning Perl" through the looking glass; instead of teaching you about programming fundamentals from scratch, it uses what you know about Perl to teach you, well, the fundamentals that make up the fundamentals. Then, these crucial grounds covered, the book suddenly leaps snarling at you, burying you under the likes of binary trees, heaps, and bubble sorts, but the whole time, you see the $s and %s and eval{}s in the example code, and know that you've never left home. This thing is still speaking your language, as alien as the concepts might seem at first (as well as, for me, evocative of dimly recalled lessons from my freshman Intro to Programming courses). Here is where we see the book's true purpose and style. It is not a book about Perl, but one about computer science algorithms, with an intended audience of Perl programmers, using the peculiar code symbols and idioms they know and love to teach them about these ancient and venerable techniques. As soon as I fully understood this, all my apprehensions about it washed away. It was nice.

(I'll note here that, even after this epiphany, I still missed the presence of exercises. I don't hold it against this book for lacking them, since it seems that few computer books, even some really fine ones, include them (and they would be less appropriate in pure-reference manuals like the Camel and Ram books), but in my opinion nothing hammers in a concept in a computer book like follow-up exercises crafted by the author of that concept's chapter. The exercises in ORA's "Learning Perl" constitute the main reason I keep shoving my now severely beaten-up copy of it at any curious Perl newbie who asks me about my language of choice. While MAWP is all about abstracts rather than specifics, I would have liked at least few puzzles thrown in to make sure that I could personally demonstrate everything it was showing me.)

After this fierce introduction of all the classic basics, including a couple of very hefty chapters on searching and sorting, the book enters into a sort of programmer's almanac mode, wandering around various subjects until it's run out of things to talk about. Just as I'm finishing up the grittiest parts of a lengthy section on matrices and graphs, trying not to make my lowly web-hacker's mind scream and run away at the very sight of the word 'eigenvalues', I tumble into new chapter, all about -- strings! Ah, lovely strings. I know strings well, yes, and regular expressions. And-- say, I didn't know you could do that....

The chapters themselves sometimes differ strongly in style, perhaps reflecting the styles of the book's multiple authors (all of whose names should be quite familiar to anyone who frequents the Comprehensive Perl Archive Network). That one on matrices, for example, seems much more in the style of a UNIX man page than the others, offering its code solutions through use of existing CPAN-available modules, rather than illustrating the Perl primitives that comprise them, but this does not diminish the value derivable from its excellent presentation of its topic's theories and concepts.

To be honest, after a while I found myself skimming and sometimes skipping right over all the chapters' blocks of source code, content merely to mentally absorb the well-described generalized goals, and not concerning myself so much with the specific Perl implementations of same, knowing that later on, when I'm at the keyboard and really need to know exactly how something works, I can confidently look it up in the index again. In this fashion, MAWP's source code is a little like that seen in ORA's big Perl offering from 1998, "The Perl Cookbook", except with recipes serving as examples of generic solutions, rather than ready-made incantations for various situations.

This is not to say that the book brushes aside implementation. Algorithms are all about efficiency, and all the theories in the world won't help if your code is clunky. Just like how my prof back in that computer course started the semester off by making sure all the students were comfy with the otherwise orthogonal tasks of logging into the school's mainframe and using a text editor, so does this book prepare its readers by introducing them to some Perlish tools, such as the standard Benchmark module, as well as terminology and symbols used in the algorithmic world to describe a technique's relative performance time, which the book's authors use consistently from then on.

Indeed, the focus on efficiency may be the best practical reason for all the book's sample source code. Given a good, generalized description of an algorithmic method, any intermediate programmer can code up its approximation in their favorite language, but if done so inefficiently, any performance gains lent by using the algorithm can get dulled. MAWP's frequent source code illustrations show how a Perl programmer might best implement the ideals discussed in the book's prose. Quite often, the book takes the time to demonstrate how to perform a certain subroutine's task using both recursive (easier to write and read, but slower) and iterative (more convoluted, but far faster if written properly) styles. In true Perlish fashion, the book makes known that there's more than one way to do it, and leaves it up to the programmer to choose which style best fits the situation at hand.

All in all, this book speaks directly to my type of programmer, teaching concepts that might otherwise lurk out of my reach by cooing gently at me in a language which, while not exactly baby talk, remains one an untrained but somewhat experienced hacker like myself can comfortably grok. To the thirstily curious, I can easily see the path between this book and something like Knuth's 'The Art of Computer Programming' to be a slippery slope (or at the very least, a passable incline). I may take that path later, but for now, I will consider MAWP a real treasure of my bookshelf.

All content on this page is copyright © 2000 by Jason McIntosh.

Return to: Reviews |

All content of this website is copyright © 1999-2016 by Jason McIntosh except where noted.

This particular page was last modified: Thu Nov 17 00:20:27 2005 (Dynamic content may be more recent than this.)

Return to home.