Best Pratice Perl
The OSCON conference is off to a good start, and the
first session I attended was Damian Conway’s Best
Practice Perl. I had heard that Conway was a terrific speaker,
and after having attended my first talk, I would have to agree.
Needless to say he had a lot of interesting things to say but here’s
some highlights of the bits I found most interesting:
- For data sets, attempt to format them so they look like a table.
It makes a big step forward in readability.
- I generally haved tried to name my functions with verbs, and
variables with nouns. He expounded on that quite a bit, suggesting
things such as:
- verb_noun_preposition (e.g., get_record_for)
- verb_noun_participle (e.g., get_record_using)
After thinking about it for a second, I liked it.
- verb_noun_preposition (e.g., get_record_for)
- He pointed out that when you localize a variable, you *don’t* get
a copy of the variable’s value (as I and a lot of other people thougth
you did). Instead you need to dolocal $Pkg::Foo = $Pkg::Foo.
- Several cool modules that I hadn’t heard about where pointed out,
including:
- List::Util - May cool operators for
working on lists. What Damain said “was left out of Perl.”
- Scalar::Util’s - in particular the weaken function
- Readonly - like use CONSTANT but allows for variable
interpolation & can be used to build-up very complex Regexes.
- Regexp::Common
- List::Util - May cool operators for
- Nested ternary blocks can be particularly useful for assign
initial defaults. E.g.:my $foo = $n 0 && $n 10 && $n
It took me a second but I liked this. The big advantage here, as
pointed out by Damain was that a value has to be assigned.
With an if/elsif/etc. construct, having a final else cannot be enforced.
- Have a POD template for for modules & applications.
- POD’s =for doesn’t display when a user perldoc’s your manual and
is a good place to include longer comments explaining the rational and
work-arounds or suggesting possible optimization & improvements.
- Don’t use prototypes in Perl. I never liked them because they’re
not what most people thing they are, but Damian pointed out several other
reasons as well, including that they’re ignored when subroutines are
invoked as methods and, based on where the prototype is declared, can
be ignored in other circumstancs as well.
- A bare
returnis better thanreturn undef,
as it’s content-sensitive and yields () in list context & undef in
scalar.
- Somewhere around 5.5.3 lexical filehandles are supported. (Very cool.)
- He’ll be releasing IO::Prompt. This will make it easier to
handle input from users (esp STDIN). In the meantime, check if a
program is being called in interactive mode & prompt instead of just
sitting there waiting for input.
- The regex /m & /x flags are going to be defaults in Perl 6.
- Gave a good reminder to use ?> to turn of backtracking. If you
don’t need backtracking, this can be a big win.
