I've heard a lot about Perl::Critic, and it has always sounded neat. Chris Dolan gave a talk about it at YAPC, and I decided I should give it a go. I didn't get around to it until this week. I went through and installed a perl-critic.t in all my distributions, then started testing them. Nearly all the violations it found were in a few groups.
"Don't use stringy eval!" Ok, but I'm trying to load a module by name, and I don't want to use UNIVERSAL::require. I need to use stringy eval!
"Don't put any code before use strict
!" Ok, that's reasonable. I'll just
move use strict
to the very top of every file. "Now don't put any code
before the package declaration!" What? No, it doesn't matter! It's lexical
to the file!
The other things it caught were pretty reasonable, and I fixed them. I just
didn't like the idea of putting no critic
comments at places where I wanted
to keep my code the same. When I looked at the code for policies, I was
surprised at how simple it was. PPI exposes a really reasonable DOM, and I was
able to code lots of fiddly little policy tweaks for my own use.
I have a few more policies I want to write, but so far I'm pretty happy with Perl::Critic and my own attempts to train its taste.
In file Foo.pm:
sub frobulator{ print shift; }
our $SHIZZLE = 42;
package Foo;
sub foo { return main::frobulator( @_ ) }
In file my_script.pl:
use Foo;
Foo::foo( $SHIZZLE );
Since $SHIZZLE and &frobulator are declared before the package statement in Foo.pm, they wind up in the caller's namespace (main). So Foo::foo will only work if it is loaded by main. And when my_script starts loading other libraries that might also load Foo, then it become a big steaming pile.
I trully hope that sane developers will never encounter this problem. But I'm frequently surprised how much insanity there is in the world