Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Perl Best Practices

by TheDamian (Priest)
on Jul 18, 2005 at 04:47 UTC ( #475641=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl Best Practices
in thread Perl Best Practices

I find it interesting that so few of these points are Perl-specific.
It's interesting you mention that. The original set of ten I had planned to use were much more Perl-centric (so much so that we wouldn't have called them "Essential Development Practices", but rather "Essential Perl Coding Practices"):
  1. Always use strict and use warnings.
  2. Use grammatical templates when forming identifiers.
  3. Use lexical variables, not package variables.
  4. Label every loop that is exited explicitly, and every next, last, or redo.
  5. Don't use bareword filehandles; use indirect filehandles.
  6. In a subroutine, always unpack @_ first, using a hash of named arguments if there are more than three parameters.
  7. Always return via an explicit return.
  8. Always use the /x ,/m , and /s flags, and the \A and \z anchors.
  9. Use capturing parentheses in regexes only when deliberately capturing, then give the captured substrings proper names.
  10. Never make variables part of a module's interface.

But as I started extracting these guidelines from the book, I realized that they relied on the reader understanding and accepting other, earlier guidelines, which in turn relied on other guidelines, etc. etc. The book is deliberately put together that way, so that the recommendations work harmoniously together and form a coherent approach to Perl programming. Unfortunately that made it hard to isolate just ten of the Perl-specific guidelines and still have them make sense.

That said, if you want to get a better sense of the (great majority of) Perl-specific advice in the book, take a look at the beta chapter we have online.

Things like "write tests first", "use revision control", and "no premature optimization" are almost universal recommendations...
...that people universally don't follow. Hence we thought these ten, though not especially Perlish, would still be a valuable reminder. ;-)


Comment on Re^2: Perl Best Practices
Re^3: Perl Best Practices
by wazoox (Prior) on Jul 18, 2005 at 10:14 UTC

    Your article is so great I declared it mandatory reading for our developers and interns :) There's only one point I'd discuss, it's the "always use exceptions" rule. Exceptions suffer exceptions :), for instance for programs that mustn't die whatever happens (daemons, GUI tools for complex jobs, etc).

    And concerning the 8th perl coding practice ( Always use the /x ,/m , and /s flags, and the \A and \z anchors.): could you explain it a bit?

      Exceptions suffer exceptions :), for instance for programs that mustn't die whatever happens
      In which case the top level of the program needs to be running inside an eval{...} anyway, to protect it from intrinsic exceptions like divide-by-zero or call-on-missing-method. So exceptions are still okay, as they'll always be caught.
      And concerning the 8th perl coding practice: could you explain it a bit?
      Sure, but an adequate explanation is about 30 pages long. Which is why I put it in a handy book, very soon to be available from most good bookstores. ;-)

      Briefly, always using /x allows you to set out regexes readably, and to comment them. Always using /ms makes Perl regexes work the way most people expect them to work, and prevents very subtle edge-case errors. Using \A and \z is essential for saying what you mean under /m (which you'll always be using).

        In which case the top level of the program needs to be running inside an eval{...} anyway (...)

        Mmmmmphf. Weren't exceptions about "less clutter" and "more readable code"? :)
        Another thing that bothers me with exceptions is that you can't see at the first glance which error will be correctly handled, or will simply receive the default treatment. After all you'll end up with special case code for every important error, whichever error management system you choose :)

        Sure, but an adequate explanation is about 30 pages long. Which is why I put it in a handy book, very soon to be available from most good bookstores. ;-)

        Be sure I'll buy it very quickly as soon as I'll have achieved "Object Oriented Perl" (mmmh, chapter 5 right now) :) Thanks for the brief explanation anyway... I have the howl book ("mastering regular expressions"), but missed time to master it yet :)

Re^3: Perl Best Practices
by simonm (Vicar) on Jul 19, 2005 at 00:28 UTC
    The original set of ten I had planned to use were much more Perl-centric (so much so that we wouldn't have called them "Essential Development Practices", but rather "Essential Perl Coding Practices")

    That sounds like it'd also be a good article, although I understand your point about the additional context required to undetsand these more Perl-specific points.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-07-26 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (177 votes), past polls