=head1 NAME
perlstyle - Perl style guide
=head1 DESCRIPTION
Each programmer will, of course, have his or her own preferences in
regards to formatting, but there are some general guidelines that will
make your programs easier to read, understand, and maintain.
The most important thing is to use L and L in all your
code or know the reason why not to. You may turn them off explicitly for
particular portions of code via C or C, and this
can be limited to the specific warnings or strict features you wish to
disable. The B<-w> flag and C<$^W> variable should not be used for this
purpose since they can affect code you use but did not write, such as
modules from core or CPAN.
Regarding aesthetics of code lay out, about the only thing Larry
cares strongly about is that the closing curly bracket of
a multi-line BLOCK should line up with the keyword that started the construct.
Beyond that, he has other preferences that aren't so strong:
=over 4
=item *
4-column indent.
=item *
Opening curly on same line as keyword, if possible, otherwise line up.
=item *
Space before the opening curly of a multi-line BLOCK.
=item *
One-line BLOCK may be put on one line, including curlies.
=item *
No space before the semicolon.
=item *
Semicolon omitted in "short" one-line BLOCK.
=item *
Space around most operators.
=item *
Space around a "complex" subscript (inside brackets).
=item *
Blank lines between chunks that do different things.
=item *
Uncuddled elses.
=item *
No space between function name and its opening parenthesis.
=item *
Space after each comma.
=item *
Long lines broken after an operator (except C and C).
=item *
Space after last parenthesis matching on current line.
=item *
Line up corresponding items vertically.
=item *
Omit redundant punctuation as long as clarity doesn't suffer.
=back
Larry has his reasons for each of these things, but he doesn't claim that
everyone else's mind works the same as his does.
Here are some other more substantive style issues to think about:
=over 4
=item *
Just because you I do something a particular way doesn't mean that
you I do it that way. Perl is designed to give you several
ways to do anything, so consider picking the most readable one. For
instance
open(my $fh, '<', $foo) || die "Can't open $foo: $!";
is better than
die "Can't open $foo: $!" unless open(my $fh, '<', $foo);
because the second way hides the main point of the statement in a
modifier. On the other hand
print "Starting analysis\n" if $verbose;
is better than
$verbose && print "Starting analysis\n";
because the main point isn't whether the user typed B<-v> or not.
Similarly, just because an operator lets you assume default arguments
doesn't mean that you have to make use of the defaults. The defaults
are there for lazy systems programmers writing one-shot programs. If
you want your program to be readable, consider supplying the argument.
Along the same lines, just because you I omit parentheses in many
places doesn't mean that you ought to:
return print reverse sort num values %array;
return print(reverse(sort num (values(%array))));
When in doubt, parenthesize. At the very least it will let some poor
schmuck bounce on the % key in B.
Even if you aren't in doubt, consider the mental welfare of the person
who has to maintain the code after you, and who will probably put
parentheses in the wrong place.
=item *
Don't go through silly contortions to exit a loop at the top or the
bottom, when Perl provides the C operator so you can exit in
the middle. Just "outdent" it a little to make it more visible:
LINE:
for (;;) {
statements;
last LINE if $foo;
next LINE if /^#/;
statements;
}
=item *
Don't be afraid to use loop labels--they're there to enhance
readability as well as to allow multilevel loop breaks. See the
previous example.
=item *
Avoid using C (or C