Book Review: Perl Best Practices
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 email@example.com.
Webinar: 8 Signs You’re Beyond Cron
On Demand NOW
|New Linux Based OS Brings Internet of Things Closer to Reality||May 27, 2015|
|Non-Linux FOSS: All the Bitcoin, None of the Bloat||May 26, 2015|
|Dr Hjkl on the Command Line||May 21, 2015|
|Initializing and Managing Services in Linux: Past, Present and Future||May 20, 2015|
|Goodbye, Pi. Hello, C.H.I.P.||May 18, 2015|
|Using Hiera with Puppet||May 14, 2015|
- New Linux Based OS Brings Internet of Things Closer to Reality
- Dr Hjkl on the Command Line
- Initializing and Managing Services in Linux: Past, Present and Future
- Non-Linux FOSS: All the Bitcoin, None of the Bloat
- Using Hiera with Puppet
- Goodbye, Pi. Hello, C.H.I.P.
- Gartner Dubs DivvyCloud Cool Cloud Management Vendor
- Infinite BusyBox with systemd
- It's Easier to Ask Forgiveness...
- Urgent Kernel Patch for Ubuntu