DVK
DVK

Reputation: 129413

What is best practice as far as using perl-isms (idiomatic expressions) in Perl?

A couple of years back I participated in writing the best practices/coding style for our (fairly large and often Perl-using) company. It was done by a committee of "senior" Perl developers.

As anything done by consensus, it had parts which everyone disagreed with. Duh.

The part that rubbed wrong the most was a strong recommendation to NOT use many Perlisms (loosely defined as code idioms not present in, say C++ or Java), such as "Avoid using '... unless X;' constructs".

The main rationale posited for such rules as this one was that non-Perl developers would have much harder time with the Perl code base otherwise. The assumption here I guess is that Perl code jockeys are rarer breed overall - and among new hires to the company - than non-Perlers.

I was wondering whether SO has any good arguments to support or reject this logic... it is mostly academic curiosity at this point as the company's Perl coding standard is ossified and will never be revised again as far as I'm aware.

P.S. Just to be clear, the question is in the context I noted - the answer for an all-Perl smaller development shop is obviously a resounding "use Perl to its maximum capability".

Upvotes: 10

Views: 1149

Answers (6)

daotoad
daotoad

Reputation: 27183

What sort of perlisms do you mean?

Good:

  • idiomatic for loops: for(1..5) {} or for( @foo ) {}
  • Scalar context evaluation of arrays: my $count = @items;
  • map, grep and sort: my %foo = map { $_->id => $_ } @objects;

OK if limited:

  • statement modifier control - trailing if, unless, etc.
    • Restrict to error trapping and early returns. die "Bad juju\n" unless $foo eq 'good juju';
    • As Schwern pointed out, another good use is conditional assignment of default values: my $foo = shift; $foo = 'blarg' unless defined $foo;. This usage is, IMO, cleaner than a my $foo = defined $_[0] ? shift : 'blarg';.
    • Reason to avoid: if you need to add additional behaviors to the check or an else, you have a big reformatting job. IMO, the hassle to redo a statement (even in a good editor) is more disruptive than typing several "unnecessary" blocks.
  • Prototypes - use only to create filtery functions like map. Prototypes are compiler hints not 'prototypes' in the sense of any other language.
  • Logical operators - standardize on when to use and and or vs. && and ||. All your code should be consistent. Best if you use a Perl::Critic policy to enforce.

Avoid:

  • Local variables. Dynamic scope is damn weird, and local is not the same as local anywhere else.
  • Package variables. Enables bad practices. If you think you need globally shared state, refactor. If you still need globally shared state, use a singleton.
  • Symbol table hackery

Upvotes: 6

Christopher Cashell
Christopher Cashell

Reputation: 800

Pick up a copy of Effective Perl Programming: Ways to Write Better, More Idiomatic Perl (2nd Edition), and treat that as a guideline. It contains many of the better idioms and is packed with the little bits of information that will get you writing good Perl style Perl code, as opposed to C or Java (or whatever) style Perl code.

Upvotes: 0

Evan Carroll
Evan Carroll

Reputation: 1

I'd say Moose kills off 99.9% of Perl-isms, by convention, that shouldn't be used: symbol table hackery, reblessing objects, common blackbox violations: treating objects as arrays or hashes. The great thing, is it does all of this without taking the functionality hit of "not using it".

If the "perl-isms" you're really referring to are mutator form (warn "bad idea" unless $good_idea), unless, and until then I don't think you really have much of an argument because these "perlisms" don't seem to inhibit readability to either perl users, or non-perl users.

Upvotes: 1

brian d foy
brian d foy

Reputation: 132822

I write code assuming that a competent Perl programmer will be reading it. I don't go out of my way to be clever, but I don't dumb it down either.

If you're writing code for people who don't know the language, you're going to miss most of the point of using that language. I often find that people want to outlaw Perlisms because they refuse to learn any more than they already know.

Since you say that you are in a small Perl shop, it should be pretty easy to ask the person who wrote the code what it means if you don't understand it. That sort of stuff should come up in code reviews and so on. Everyone continues to learn more about the language as you have periodic and regular chances to review the code. You shouldn't let too much time elapse without other eyeballs looking at someone's code. You certainly shouldn't wait until a week after they leave the company.

As for new hires, I'm always puzzled why anyone would think that you should sit them in front of a keyboard and turn them loose expecting productive work in a codebase they have never seen.

This isn't limited to Perl, either. It's a general programming issue. You should always be learning more about your tools. Most of the big shops I know have mini-bootcamps to bring developers up to speed on the codebase, including any bits of tricky code they may encounter.

Upvotes: 30

martin clayton
martin clayton

Reputation: 78135

It must have been, as you say, a few years ago, because Damian Conway has 'cornered the market' in Perl standards with Perl Best Practices for the last few years.

I've worked in a similarly ossified environment - where we were not allowed to adopt the latest best practices, because that would be a change, and no one at a sufficiently high level in the corporate structure understood (or could be bothered to understand) Perl and sign off on moving in to the 21st Century.

A corporation that deploys a technology and retains it, but doesn't either buy in expertise or train up in house, is asking for trouble.

(I'd guess you're working in a highly change-controlled environment - financial perhaps?)

I agree with brian on this by the way.

Upvotes: 2

Dan
Dan

Reputation: 11069

I ask myself two simple questions:

  • Am I doing this because it's devilishly clever and/or shows off my extensive knowledge of Perl arcana?

Then it's a bad idea. But,

  • Am I doing this because it's idiomatic Perl and benefits from Perl's distinct advantages?

Then it's a good idea.

I see no justifiable reason to reject, say, string interpolation just because Java and C don't have it. unless is a funny one but I think having a subroutine start with the occasional

return undef unless <something>;

isn't so bad.

Upvotes: 7

Related Questions