| Title: | Perl Best Practices |
| Authors: | Damian Conway |
| Publisher: | O'Reilly |
| Pages: | 517 (w/ index) |
| Reviewer: | Andy Bach |
| Synopsis: | A excellent short, simple guide to the 'best' way to write perl - think Strunk&White for JAPHs. To quote D.C. 'to help you move beyond the illusion of the sensual programming life, and become stylistically enlightened.' |
| Table of Contents | http://www.oreilly.com/catalog/perlbp/toc.html |
Each chapter (like I/O, Built-in Functions, Regular Expressions) is broken down into sections that cover a particular aspect (for I/O, its Filehandles, Indirect Filehandles, Localizing Filehandles thru Progress Indicators and Autoflushing) of the main. He writes 'bad' code, shows where its troubles lie and redoes the sample in 'correct' style (enough w/ the scare quotes - good and bad hereonforward are Conway's vs anarchy). More than once he hides a different error in the original code to show how the better style makes the subtle bug stand out more easily.
You may not agree with every guideline he makes. For example, for if/then/else indenting, Conway suggests:
if ( this ) {
do_something();
}
else {
do_something_else();
}
is more readable (the else is on the same level as the matching 'if') and
so worth the wasted line (esp. as:
if ( this ) {
...
} # if this
elsif ( that ) {
...
} # if that
elsif { theother ) {
...
} # if the other
else {
...
} # else
which lets you nicely comment, a suggestion I'm still working on.
He does say this book is, if nothing else, a starting point for discussion
about shared programming guidelines, but he does think that nearly all of
the book's guidelines are as good as it gets.
His focus is on maintainability; writing code that is clear enough in layout to help, as much as possible, the next iteration of reader (either you, three weeks later or another programmer one job change later) to be able to determine the point of the code. Call it layout or whitespace usage or just style, he makes a strong case that some choices are just much more readable and much less confusing than others, whether for a while loop or nested triene (the 'pitchfork' ... ? ... : ...) operators.
He does cover a number of places where unclear standards (like return vs return undef vs return 0) can introduce subtle bugs that you don't want to try and find. These sections have the double value of keeping you out of trouble and, often, teaching new Perl ideas. Even if you disagree and ignore every one of these guidelines (you won't, or, rather if you do, you deserve whatever you get) you'll still improve your coding and Perl understanding.
Best of all, this is one of those books you can enjoy reading. Conway is a good, clear writer and teacher but he's also very funny. It's a Perl geek sort of funny, but rrom footnotes to variable names, his dry, Australian wit (that's probably redundant) makes it a very pleasant read.
Great backbeat and though it'll be a little hard to dance to (ending all regular expressions with /xms???), but well worth it. I give it a 10.
