Review: Perl Best Practices

275

Author: Joe 'Zonker' Brockmeier

Damian Conway’s Perl Best Practices is not your standard “learn to program” Perl book. Think of it as The Elements of Style for Perl — the book will help you to write Perl programs that are easier to read and maintain and less likely to have errors.

The format of the book is simple. Conway starts each section with a rule, “do this” or “don’t do this,” then explains his reasoning and (when appropriate) provides examples of Perl code that follows the directive, as well as examples of code that doesn’t.

Perl Best Practices is a good “browsing” book. Have five or 10 minutes to kill? Flip to a random page and read a couple of Conway’s guidelines. As Conway mentions in the book, most people develop a style that feels right to them. These habits will be hard to break, so there’s not much point in sitting down and trying to read the book straight through. Better to read a few practices at a time and try to improve those habits (if necessary) and move on.

What the book covers

The book comprises 19 chapters, and covers everything from layout and naming conventions to best practices for object-oriented programming in Perl.

The world would probably be a better place if all Perl hackers followed the guidelines in chapters two and three, “Code Layout” and “Naming Conventions.” These tackle the best practices to make Perl programs easier to read and maintain. While it’s possible to write very compact Perl by making use of Perl’s unique syntax, it’s also possible to write code that’s nearly incomprehensible — though functional. By following Conway’s guidelines, developers can help ensure that their code is more easily understood.

The book tackles better methods of writing control structures — if, until, unless, and so forth — and when to avoid them altogether. For example, Conway advises that programmers use if and while, rather than unless or until, because the “negative” control structures will make things more difficult for developers who are used to languages that do not have negative conditional tests.

Of course, every Perl book has to have a chapter dealing with regular expressions. In Perl Best Practices, we get coverage of the best way to write regular expressions that will be predictable in what they match and easy for the next person to read. If you’ve ever tried to read someone else’s program that has a lot of regular expressions, you’ll definitely appreciate this chapter.

The book also covers best practices for handling documentation in code, testing and debugging, error handling, using Perl modules and built-in functions, and much more. The chapters are organized by topic, rather than importance — but Appendix A breaks them down into the 10 best development practices, 10 best coding practices, and 10 best module practices. Appendix B, “Perl Best Practices,” breaks the guidelines down into a set of one-liners, in the order that they appear in the book. This might be the best place to start for readers who prefer to skim technical titles.

A fair amount of the advice in the book would apply to programming in any language. For example, many of the guidelines in chapter 18 (“Testing and Debugging”) would apply just as well to PHP, Python, or C. But the bulk of the book is specific to Perl. It would be nice to see similar titles for other programming languages — Perl wonks aren’t the only programmers who could do with a style guide for programming.

Summary

Perl Best Practices is an excellent book. If you spend a lot of time working with Perl, and want to invest some time in writing better code, this book is definitely for you.

The book is not for casual Perl programmers or beginners. If you don’t spend a significant amount of time writing Perl programs, it’s probably not worth picking up. Yes, some of the suggestions in the book would be helpful — but if you’re only writing the occasional quick and dirty script (as many do) it’s probably not worth the investment. It’s probably a bit too deep for new Perl programmers. The book is well-written, but doesn’t spend time covering the basics, so readers won’t get the most out of the book unless they’re reasonably fluent in Perl to begin with.

I should also mention that I enjoyed Conway’s writing style. The book is written in a conversational tone, with a little humor thrown in for good measure.

The only real problem with Perl Best Practices is that there’s no way to force other Perl programmers to read it.

Title Perl Best Practices
Publisher O’Reilly
Authors Damian Conway
ISBN 0596001738
Pages Paperback, 542 pages
Rating 9 out of 10
Summary Guidelines for writing better Perl code
Price (retail) $39.95 Buy it online

Category:

  • Perl