Review
Book Description
Product Description
Many programmers code by instinct, relying on convenient habits or a "style" they picked up early on. They aren't conscious of all the choices they make, like how they format their source, the names they use for variables, or the kinds of loops they use. They're focused entirely on problems they're solving, solutions they're creating, and algorithms they're implementing. So they write code in the way that seems natural, that happens intuitively, and that feels good.
But if you're serious about your profession, intuition isn't enough. Perl Best Practices author Damian Conway explains that rules, conventions, standards, and practices not only help programmers communicate and coordinate with one another, they also provide a reliable framework for thinking about problems, and a common language for expressing solutions. This is especially critical in Perl, because the language is designed to offer many ways to accomplish the same task, and consequently it supports many incompatible dialects.
With a good dose of Aussie humor, Dr. Conway (familiar to many in the Perl community) offers 256 guidelines on the art of coding to help you write better Perl code--in fact, the best Perl code you possibly can. The guidelines cover code layout, naming conventions, choice of data and control structures, program decomposition, interface design and implementation, modularity, object orientation, error handling, testing, and debugging.
They're designed to work together to produce code that is clear, robust, efficient, maintainable, and concise, but Dr. Conway doesn't pretend that this is the one true universal and unequivocal set of best practices. Instead, Perl Best Practices offers coherent and widely applicable suggestions based on real-world experience of how code is actually written, rather than on someone's ivory-tower theories on how software ought to be created.
Most of all, Perl Best Practices offers guidelines that actually work, and that many developers around the world are already using. Much like Perl itself, these guidelines are about helping you to get your job done, without getting in the way.
Praise for Perl Best Practices from Perl community members:
"As a manager of a large Perl project, I'd ensure that every member of my team has a copy of Perl Best Practices on their desk, and use it as the basis for an in-house style guide." -- Randal Schwartz
"There are no more excuses for writing bad Perl programs. All levels of Perl programmer will be more productive after reading this book." -- Peter Scott
"Perl Best Practices will be the next big important book in the evolution of Perl. The ideas and practices Damian lays down will help bring Perl out from under the embarrassing heading of "scripting languages". Many of us have known Perl is a real programming language, worthy of all the tasks normally delegated to Java and C++. With Perl Best Practices, Damian shows specifically how and why, so everyone else can see, too." -- Andy Lester
"Damian's done what many thought impossible: show how to build large, maintainable Perl applications, while still letting Perl be the powerful, expressive language that programmers have loved for years." -- Bill Odom
"Finally, a means to bring lasting order to the process and product of real Perl development teams." -- Andrew Sundstrom
"Perl Best Practices provides a valuable education in how to write robust, maintainable Perl, and is a definitive citation source when coaching other programmers." -- Bennett Todd
"I've been teaching Perl for years, and find the same question keeps being asked: Where can I find a reference for writing reusable, maintainable Perl code? Finally I have a decent answer." -- Paul Fenwick
"At last a well researched, well thought-out, comprehensive guide to Perl style. Instead of each of us developing our own, we can learn good practices from one of Perl's most prolific and experienced authors. I recommend this book to anyone who prefers getting on with the job rather than going back and fixing errors caused by syntax and poor style issues." -- Jacinta Richardson
"If you care about programming in any language read this book. Even if you don't intend to follow all of the practices, thinking through your style will improve it." -- Steven Lembark
"The Perl community's best author is back with another outstanding book. There has never been a comprehensive reference on high quality Perl coding and style until Perl Best Practices. This book fills a large gap in every Perl bookshelf." -- Uri Guttman
From the Publisher
About the Author
Damian Conway holds a PhD in Computer Science and is an honorary Associate Professor with the School of Computer Science and Software Engineering at Monash University, Melbourne, Australia.
Currently he runs an international IT training company--Thoughtstream--which provides programmer development from beginner to masterclass level throughout Europe, North America, and Australasia.
Damian was the winner of the 1998, 1999, and 2000 Larry Wall Awards for Practical Utility. The best technical paper at the annual Perl Conference was subsequently named in his honour. He is a member of the technical committee for The Perl Conference, a keynote speaker at many Open Source conferences, a former columnist for The Perl Journal, and author of the book Object Oriented Perl. In 2001 Damian received the first "Perl Foundation Development Grant" and spent 20 months working on projects for the betterment of Perl.
A popular speaker and trainer, he is also the author of numerous well-known Perl modules, including Parse::RecDescent (a sophisticated parsing tool), Class::Contract (design-by-contract programming in Perl), Lingua::EN::Inflect (rule-based English transformations for text generation), Class::Multimethods (multiple dispatch polymorphism), Text::Autoformat (intelligent automatic reformatting of plaintext), Switch (Perl's missing case statement), NEXT (resumptive method dispatch), Filter::Simple (Perl-based source code manipulation), Quantum::Superpositions (auto-parallelization of serial code using a quantum mechanical metaphor), and Lingua::Romana::Perligata (programming in Latin).
Most of his time is now spent working with Larry Wall on the design of the new Perl 6 programming language.
Excerpted from Perl Best Practices by Damian Conway. Copyright © 2005. Reprinted by permission. All rights reserved.
If you have a procedure with ten parameters, you probably missed some.
Alan Perlis
Subroutines are one of the two primary problem-decomposition tools available in Perl, modules being the other. They provide a convenient and familiar way to break a large task down into pieces that are small enough to understand, concise enough to implement, focused enough to test, and simple enough to debug.
In effect, subroutines allow programmers to extend the Perl language, creating useful new behaviours with sensible names. Having written a subroutine, you can immediately forget about its internals, and focus solely on the abstracted process or function it implements.
So the extensive use of subroutines helps to make a program more modular, which in turn makes it more robust and maintainable. Subroutines also make it possible to structure the actions of programs hierarchically, at increasingly high levels of abstraction, which improves the readability of the resulting code.
Thats the theory, at least. In practice, there are plenty of ways that using subroutines can make code less robust, buggier, less concise, slower, and harder to understand. The guidelines in this chapter focus on avoiding those outcomes.
Call Syntax
Call subroutines with parentheses but without a leading &
Its possible to call a subroutine without parentheses, if it has already been declared in the current namespace:
sub coerce
# and later...
my $expected_count = coerce $input, $INTEGER, $ROUND_ZERO;
But that approach can quickly become much harder to understand:
fix my $gaze, upon each %suspect;
More importantly, leaving off the parentheses on subroutines makes them harder to distinguish from builtins, and therefore increases the mental search space when the reader is confronted with either type of construct. Your code will be easier to read and understand if the subroutines always use parentheses and the built-in functions always dont:
my $expected_count = coerce($input, $INTEGER, $ROUND_ZERO);
fix(my $gaze, upon(each %suspect));
Some programmers still prefer to call a subroutine using the ancient Perl 4 syntax, with an ampersand before the subroutine name:
&coerce($input, $INTEGER, $ROUND_ZERO);
&fix(my $gaze, &upon(each %suspect));
Perl 5 does support that syntax, but nowadays its unnecessarily cluttered. Barewords is forbidden under use strict, so there are far fewer situations in which a subroutine call has to be disambiguated.
On the other hand, the ampersand itself is visually ambiguous; it can also signify a bitwise AND operator, depending on context. And context can be extremely subtle:
$curr_pos = tell &get_mask( ); # means: tell(get_mask( ))
$curr_time = time &get_mask( ); # means: time() & get_mask( )
Prefixing with & can also lead to other subtle (but radical) differences in behaviour:
sub fix {
my (@args) = @_ ? @_ : $_; # Default to fixing $_ if no args provided
# Fix each argument by grammatically transforming it and then printing it...
for my $arg (@args) {
$arg =~ s/\A the \b/some/xms;
$arg =~ s/e \z/es/xms;
print $arg;
}
return;
}
# and later...
&fix('the race'); # Works as expected, prints: 'some races'
for ('the gaze', 'the adhesive') {
&fix; # Doesn't work as expected: looks like it should fix($_),
# but actually means fix(@_), using this scope's @_!
# See the 'perlsub' manpage for details
}
All in all, its clearer, less ambiguous, and less error-prone to reserve the &subname syntax for taking references to named subroutines:
set_error_handler( \&log_error );
Just use parentheses to indicate a subroutine call:
coerce($input, $INTEGER, $ROUND_ZERO);
fix( my $gaze, upon(each %suspect) );
$curr_pos = tell get_mask( );
$curr_time = time & get_mask( );
And always use the parentheses when calling a subroutine, even when the subroutine takes no arguments (like get_mask( )). That way its immediately obvious that you intend a subroutine call:
curr_obj( )->update($status); # Call curr_obj( ) to get an object,
# then call the update( )method on that object
and not a typename:
curr_obj->update($status); # Maybe the same (if currobj( ) already declared),
# otherwise call update( ) on class 'curr_obj'
Homonyms
Dont give subroutines the same names as built-in functions
If you declare a subroutine with the same name as a built-in function, subsequent invocations of that name will still call the builtin...except when occasionally they dont. For example:
sub lock {
my ($file) = @_;
return flock $file, LOCK_SH;
}
sub link {
my ($text, $url) = @_;
return qq{$text};
}
lock($file); # Calls 'lock' subroutine; built-in 'lock' hidden
print link($text, $text_url); # Calls built-in 'link'; 'link' subroutine hidden
Perl considers some of its builtins (like link) to be "more built-in" than others (like lock), and chooses accordingly whether to call your subroutine of the same name. If the builtin is "strongly built-in", an ambiguous call will invoke it, in preference to any subroutine of the same name. On the other hand, if the builtin is "weakly builtin", an ambiguous call will invoke the subroutine of the same name instead.
Even if these subroutines did always work as expected, its simply too hard to maintain code where the program-specific subroutines and the languages keywords overlap:
sub crypt { return "You're in the tomb of @_\n" }
sub map { return "You have found a map of @_\n" }
sub chop { return "You have chopped @_\n" }
sub close { return "The @_ is now closed\n" }
sub hex { return "A hex has been cast on @_\n" }
print crypt( qw( Vlad Tsepes ) ); # Subroutine or builtin?
for my $reward (qw( treasure danger) ) {
print map($reward, 'in', $location); # Subroutine or builtin?
}
print hex('the Demon'); # Subroutine or builtin?
print chop('the Demon'); # Subroutine or builtin?