Book Review: Perl Best Practices

September 15th, 2005 by Paul Barry in

Perl and its supporters are known for working in whatever way suits them, but that can make for unnecessarily complex and confusing code. Here's a book, though, that dares to say "enough".
Your rating: None Average: 5 (1 vote)

Title: Perl Best Practices

Publisher: O'Reilly Media, Inc.

ISBN: 0-596-00173-8

Price: $39.95

We all have done it, haven't we? We've looked at source code--either our own or someone else's--and asked that age-old programming question: "What is this code doing?". When code is written in Perl, the aforementioned question probably is uttered more than it needs to be, as Perl--the ever-popular duct tape of the Internet--often is used to produce code that is hard to understand. This problem is exasperated by the Perl community's near obsession with letting programmers work in whatever way suits them. Although the Perl motto, "There's more than one way to do it", often can be a strength, it also can work against large projects and team efforts. This may be especially true if every team member's code is a law-unto-itself when it comes to style, coding standards, layout and idioms used. Typically, if this is the case, you end up with a Perl code base that is unmaintainable.

In tackling this problem head-on, Damian Conway is nothing if not brave. In his latest book, Perl Best Practices, Damian attacks subjects that should be close to the heart of every Perl programmer: coding standards, styles and development practices and behaviors. In 500 pages or so, Conway presents what he believes to be a consistent set of standards and styles for developing maintainable code when using Perl. Almost magically, there are 256 of these guidelines in the book, offering help to Perl programmers of every ability.

Conway's bravery comes from his decision to highlight the problems that programmers can get themselves into when programming in Perl. Typically, these problems are with Perl as opposed to the programmer, and there's plenty of criticism of the programming language adored by the majority of Conway's readership. Conway's saving grace is that, having criticized, he then proceeds to demonstrate a technique or practice for avoiding that which he criticized. Make no mistake, some of Conway's advice is controversial. Indeed, the outright banning of unless is highlighted by Conway himself as potentially raising a few eyebrows. And, there's plenty of other guidelines that will produce gasps and grunts from many a Perl programmer. As an example, consider the "Don't use bareword filehandles" guideline from the start of Chapter 10, which warns against writing code such as this:


open FILE, '<', $filename
    or croak "Can't open '$filename': $OS_ERROR";


If you are reading this and wondering what's wrong with the above code, don't despair. On the surface, nothing is too wrong with the code, as it does indeed open a file and assign it to a filehandle. What can happen, though, is this innocent-looking code can lead to problems when systems grow to anything larger than a 20-line throw-away script. It is more robust, reliable, efficient and maintainable to rewrite the above code using an indirect filehandle, like so:


open my $FILE, '<', $filename
    or croak "Can't open '$filename': $OS_ERROR";

Why it is important to do this is covered in detail by Conway. After reading his reasoning, it is hard to argue against or ignore what's being advised, even though every other Perl book I've ever seen--including mine--open files using a bareword filehandle. Of course, it is at times such as this that Conway's advice and explanations become invaluable. By describing the awfulness that can result from not adhering to the guidelines and then following up with a robust solution, many a late-night or multi-day debugging session can be avoided. Programmer sanity is, without a doubt, something to strive for and maintain. It is clear that one of Conway's main goals in writing this book is to keep you and others that read your code sane by steering the programmer away from many of Perl's pitfalls.

While working through the book, I learned a lot about the current problems with Perl 5. I found myself constantly reciting the same prayer, "Oh my, please let them fix that in Perl 6." All indications within Perl Best Practices say that these things will be fixed. However, as Perl 5 will be around for quite some time, Conway's book has real value now that will only increase as time goes by. Even when Perl 6 does arrive, the majority of the guidelines will remain valid and should continue to be followed.

Of interest is the long list of technical reviewers thanked at the start of the book: it includes some of the biggest names in the Perl world. So Perl Best Practices is not simply Conway's best practices, but the best practices actually practiced by the inner circle that Conway identifies.

One small gripe is I wish the guidelines had been numbered, for ease of reference. Nice touches include the decision to highlight "good code" in bold mono throughout the text and "bad code" in regular mono. The book also provides plenty of references to supporting material on the Web. The jokes, when they come, often are something special.

As all Perl programmers know, there's an awful lot to Perl, and there's an awful lot of Perl to learn. In addition to covering the essential material, Conway's book discusses some of the darker recesses (features?) of Perl that I've never seen covered anywhere else. By working through Perl Best Practices, you will become a better Perl programmer, even if you decide not to adopt the guidelines offered to you. From my own point of view, I'd like to see a CPAN icon that indicates whether a submitted module conforms to Conway's 256 guidelines. I'd like to build my future applications using shared modules that I know I'll be comfortable reading, amending and working with.

For now, I'm off to update my Perl Tutorial to conform to Conway's guidelines. I'm also looking forward to the first official release of Perl 6. Damian Conway's Perl Best Practices is a must-have.

Paul Barry lectures at the Institute of Technology, Carlow, in Ireland. He is the author of two textbooks that use Perl. He can be contacted at paul.barry@itcarlow.ie.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
herseybendevar's picture

0www

On September 19th, 2007 herseybendevar (not verified) says:

thanks bro nice article Paul Barry

"i hate micr0z0rt :D"

vinee's picture

Nice book

On September 3rd, 2007 vinee (not verified) says:

Nice book :)

bobby's picture

search

On August 26th, 2007 bobby (not verified) says:

nice book

Wong Seoul's picture

A must-read for any Perl programmer

On July 13th, 2007 Wong Seoul (not verified) says:

Covering the sections other Perl books do not cover..
Great Help.....
Thanks

shine's picture

Good and Informative

On May 18th, 2007 shine (not verified) says:

Nice work.. Good and Informative book ..

UK Perl Programmer's picture

Useful resource

On March 21st, 2007 UK Perl Programmer (not verified) says:

I found the book to be an interesting read, and certainly worth a look. While in The Real World I'm still guilty of ignoring some of these best practices, reading this has certainly improved the quality of my code.

Baku's picture

?

On December 8th, 2006 Baku (not verified) says:

Is this journal hosted by wordpress?

Po's picture

Comment

On December 3rd, 2006 Po (not verified) says:

Interesting article

Michael R's picture

Very good book

On November 19th, 2005 Michael R (not verified) says:

This is the book that tells you how to do things until you gain enough experience to know how to do them. Then you'll do them as described in this book because the way described is so good.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.







Read this issue