Announcing Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter

The subject Perl-Critic policy is an alternate implementation of the core policy Variables::ProhibitUnusedVariables. My implementation attempts to do a more thorough job of finding unused variables.

The details of the policy can be found at Perl-Critic-Policy-Variables-ProhibitUnusedVarsStricter. My purpose here is more to discuss why the policy exists in the form it does.

Once upon a time there was something called warnings::unused. This was an XS module that required the code under analysis to be actually run. This has since been retracted, or at least I can find nothing by that name in CPAN.

There is also a Perl-Critic policy, Variables::ProhibitUnusedVariables. This is "safe" in the sense that no code has to be run, but it is also a fairly simple policy, which by design and intent prefers false negative results to false positives.

It seems to me that in the absence of warnings::unused something more comprehensive should be out there, and embedding the tool in the Perl-Critic framework seems like the logical thing to do. But because the more aggressive analysis makes false positives more likely, this policy should not be in the core.

ProhibitUnusedVarsStricter currently exists only as a development release, which I consider to be alpha code. I plan to keep it there for a month or so, so people can bang on it and give feedback, and so I can (if need be) freely jerk it around in response to that feedback. The feedback can be any way you can get it to me. For public discussion I can't think of any better alternative than a reply to this blog.


> This has since been retracted

warnings::unused seems to be doing fine:

Wow. Mithaldu is right. This is good news!

But Tom isn't really wrong either:
AFAIK, the old version wouldn't compile with newer versions of Perl (I forget whether this was with 5.14 or 5.16).

I'm a big fan of warnings::unused and its (temporary) successor Test::Vars wasn't really apt to my requirements. I like to do a "perl -wunused::vars -c lib/Foo/" before checking stuff into version control because it helps to spot bugs. An unused variable is a strong code smell that either points to sloppy coding (usually of the copy-and-paste style) or of a clear bug.

But whatever fate and history of warnings::unused are, I think Variables::ProhibitUnusedVariables is a pretty good addition to the Perl-Critic tool kit.

Leave a comment

About Tom Wyant

user-pic Fine Perl code for over 0.005 centuries.