Perl Best Practices

Ovid on 2005-11-11T23:34:50

I recently recommended Damian's book "Perl Best Practices" to someone. Here's the response I received today:

I just wanted to thank you for pointing me to the Perl Best Practices book. It's seriously changed just about everything I think about Perl and how I approach anything, even tiny one-off scripts. module-starter is incredibly awesome, as are the inside-out objects. Seriously, it's the best Perl book I've ever flipped through simply because of how much it's changed everything for me.

If you have the book, what do you think of it? If you don't have the book, why not?


Yay RJBS and me.

petdance on 2005-11-12T01:03:38

You know I love it, but I'm even more tickled that he liked Module::Starter. Hooray for me and RJBS!

PBP

n1vux on 2005-11-12T01:05:15

I'm not completely reformed yet, but ...

I like the book so well I bought the course for my colleagues.

I like the book so well I bought a second copy to keep at the office.

Re:PBP

rats on 2005-11-12T08:21:18

PBP is so good it's hard to put in words. From now on whenever someone starts dissing Perl, talking about linenoise and More Than One Way to Shoot Yourself in the Foot, I can point them to PBP and get on with writing (good!) code.

However the book is not perfect.

I've been stung by three of Damian's recommendations so far: replacing '' with q{} (without thinking); using 'version.pm' (won't compile on Solaris); using 'Readonly' instead of 'constant' (Readonly won't compile on Solaris).

And I refuse to use '&&' and '||' and will continue to use 'and' and 'or' for exactly the reason Damian states in the heading: don't mix high and low prececence operators. 'and', 'or' and 'not' have the lowest precedence and hence there is never any confusion: every other operator has a higher precedence. I think Damian's argument for using '&&' and '||' is false.

After 20 years of using cuddled 'else's (Pascal, C and Perl) I have decided to defer to Damian's recommendation to use uncuddled ones (that's hurting the most so far :-)).

But as for s///xms, inside-out objects, Module::Starter, Smart::Comments... the list of absolute gems goes on and on. I love this book!

Re:PBP

Damian on 2005-11-12T21:16:15

I've been stung by three of Damian's recommendations so far:
While I won't be held responsible for the deficiencies of Solaris (;-), I am very keen to know how replacing '' with q{} stung you.
'and', 'or' and 'not' have the lowest precedence and hence there is never any confusion
The usual confusion is that people expect assignments and commas (especially in argument lists) to be of lower precedence than those three boolean connectives. But as long as it encouraged you to think through your decision to use them, then the book has done its job. :-)

Damian

Re:PBP

sigzero on 2005-11-13T02:10:03

Since I have yet to purchase the book what exactly does replacing " with q{} mean? Are we talking simple assignments like:

my $new_var = q{This is the var};

Or something totally different?

Re:PBP

rats on 2005-11-13T23:12:33

I could say read the book...

Damian recommends replacing empty strings (often typed as '' (singlequote-singlequote)) with q{} so you don't misread it as one doublequote (which is exactly what happened in my previous post).

Re:PBP

sigzero on 2005-11-14T02:42:57

Oh I will get the book. No doubt about that.

And the reasoning for q{} seems sound as well. That will be a re-trainer though. : )

Re:PBP -- compiles on Solaris?

n1vux on 2005-11-13T01:42:30

What do you mean by
version.pm' won't compile on Solaris ... Readonly won't compile on Solaris

Per CPAN Testers, version and Readonly compile on Solaris with 5.8.5 http://testers.cpan.org/show/version.html http://testers.cpan.org/show/readonly.html

Are you trying to use Solaris bundled 5.005_03? Trying to build C-based modules with a different compiler than Perl was built with?

Oh!

sigzero on 2005-11-12T02:05:46

I haven't been able to eek it out of my budget yet! I am working on it though.

Re:Oh!

jk2addict on 2005-11-12T02:12:32

dude... bookpool.com... 47% off O'Reilly books!

Re:Oh!

sigzero on 2005-11-12T14:50:42

dude...3 kids, 1 on the way, not a good paying job. : )

I am working on it!

What I think of PBP

tirwhan on 2005-11-12T09:58:47

I laughed, cried, fell down, my life was changed. Well, maybe I didn't cry so much ;-).

Seriously though, best perl book since I first picked up the Camel. Any project that uses PBP as a guideline is doing at least one thing right.

In a way it's a shame this came out in this form though. I'd imagine that a guide to perl best practices could best be implemented in a wiki, with arguments for and against certain practices as well as updates and additions from the community. The book would be a perfect baseline to start off from, but somehow I can't see O'Reilly/TheDamian looking too favourably on someone who'd copy the entire book into a wiki ;-).

Re:What I think of PBP

ziggy on 2005-11-14T21:32:00

In a way it's a shame this came out in this form though. I'd imagine that a guide to perl best practices could best be implemented in a wiki, with arguments for and against certain practices as well as updates and additions from the community.

PBP (any "best practices" in general) works best when it comes from a single voice. Damian synthesized a lot of experience when he condensed it into a dead tree. So this isn't one lone kook's guide to what makes for good Perl, but a coherent guide to what some of the best practitioners think makes for good Perl.

Putting all of this into a wiki is a knee-jerk good idea, but in reality doesn't have the same benefit. Sure, you may not agree with some of Damian's recommendations, but at least you can reason about what Damian's summarized and why you should deviate from that norm. Replacing that with a conglomeration of contentious permathreads on a wiki is much less useful.

Very useful read

ajt on 2005-11-12T14:09:50

I got mine this week and have been skimming through it this week before going to sleep. I can't say I agree with everything, but he argues his case and it's a mostly consistent story. Probably the best perl book I've read since Effective Perl Programming.

Re:Very useful read

ChrisDolan on 2005-11-12T20:10:23

Lately I've been contributing to the Perl::Critic project. That module analyzes your code against a collection of Policy modules, most of which are inspired by PBP. If you're interested in converting your code to Damian's recommendations, I recommend starting with Test::Perl::Critic.

Plug: we'd be thrilled to have more contributors for Policy modules! Our SVN repository is at perlcritic.tigris.org. I've added a list of wanted PBP recommendations in the TODO file

Re:Very useful read

sigzero on 2005-11-14T13:35:03

I cannot find any Windows PPMs for the modules you mention. Are they out there?

Re:Very useful read

thaljef on 2005-12-19T07:42:15

Randy Kobes has kindly posted a PPM of Perl::Critic here

Since I was so harassed!

sigzero on 2005-11-14T23:14:42

I purchased it from Bookpool today. : )

Thumbs up

revdiablo on 2005-11-15T00:33:37

PBP is the first Perl book I've purchased. I learned Perl primarily from the perldocs (combined with about 7 years of painfully banging my head on the walls), which may say more about me than it does about anything else. I never had the desire to buy a Perl book, but I'm glad I bought this one. I love it. I think if I recommend it to any more of my friends, they'll start painfully banging my head on the walls themselves. :-)