views:

476

answers:

8

One I am aware of is Perl::Critic

And my googling has resulted in no results on multiple attempts so far. :-(

Does anyone have any recommendations here?

Any resources to configure Perl::Critic as per our coding standards and run it on code base would be appreciated.

+5  A: 

There is perltidy for most stylistic standards. perlcritic can be easily configured using a .perlcritic file. I personally use the it at level one, but I've disabled a few policies.

Leon Timmermans
+4  A: 

In addition to 'automated frameworks', I highly recommend Damian Conway's Perl Best Practices. I don't agree with 100% of what he suggests, but most of the time he's bang on.

talexb
A: 

@talexb Yes, Perl Best Practices is one of the must reads for perl programmers. The reason I said automated frameworks is because generally people know what to do but having a check makes sure that we obvious diversions don't do unnoticed?

Perl::Critic is actually an attempt to enforce Conway's best practices. And even I dont agree with 100% of Conway's suggestions and that is why I need to know how to configure Perl::Critic (if this is the best framework).

Jagmal
+1  A: 

A nice combination is perlcritic with EPIC for Eclipse - hit CTRL-SHIFT-C (or your preferred configured shortcut) and your code is marked up with warning indicators wherever perlcritic has found something to complain about. Much nicer than remembering to run it before checkin. And as normal with perlcritic, it will pick up your .perlcriticrc so you can customise the rules. We keep our .perlcriticrc in version control so everyone gets the same standards.

Alex Francis
+8  A: 

In terms of setting up a profile, have you tried perlcritic --profile-proto? This will emit to stdout all of your installed policies with all their options with descriptions of both, including their default values, in perlcriticrc format. Save and edit to match what you want. Whenever you upgrade Perl::Critic, you may want to run this command again and do a diff with your current perlcriticrc so you can see any changes to existing policies and pick up any new ones.

In terms of running perlcritic regularly, set up a Test::Perl::Critic test along with the rest of your tests. This is good for new code.

For your existing code, use Test::Perl::Critic::Progressive instead. T::P::C::Progressive will succeed the first time you run it, but will save counts on the number of violations; thereafter, T::P::C::Progressive will complain if any of the counts go up. One thing to look out for is when you revert changes in your source control system. (You are using one, aren't you?) Say I check in a change and run tests and my changes reduce the number of P::C violations. Later, it turns out my change was bad, so I revert to the old code. The T::P::C::Progressive test will fail due to the reduced counts. The easiest thing to do at this point is to just delete the history file (default location t/.perlcritic-history) and run again. It should reproduce your old counts and you can write new stuff to bring them down again.

Perl::Critic has a lot of policies that ship with it, but there are a bunch of add-on distributions of policies. Have a look at Task::Perl::Critic and Task::Perl::Critic::IncludingOptionalDependencies.

You don't need to have a single perlcriticrc handle all your code. Create separate perlcriticrc files for each set of files you want to test and then a separate test that points to each one. For an example, have a look at the author tests for P::C itself at http://perlcritic.tigris.org/source/browse/perlcritic/trunk/Perl-Critic/xt/author/. When author tests are run, there's a test that runs over all the code of P::C, a second test that applies additional rules just on the policies, and a third one that criticizes P::C's tests.

I personally think that everyone should run at the "brutal" severity level, but knock out the policies that they don't agree with. Perl::Critic isn't entirely self compliant; even the P::C developers don't agree with everything Conway says. Look at the perlcriticrc files used on Perl::Critic itself and search the Perl::Critic code for instances of "## no critic"; I count 143 at present.

(Yes, I'm one of the Perl::Critic developers.)

Elliot Shank
+1  A: 

In addition to the cosmetic best practices, I always find it useful to run Devel::Prof on my unit test suite to check test coverage.

jonfm
+1  A: 

The post above mentioning Devel::Prof probably really means Devel::Cover (to get the code coverage of a test suite).

jplindstrom