I’ve been working on some linting projects at $work and one thing I’m working on is finding unused variables in our codebase. This can be pretty important in a larger codebase. When you find variables which have not been used, you may find yourself asking, “why is this here?” and you may not easily get an answer. Eliminating unused variables can be good for the performance of the code, but also good for everyone else on your team. So, we want to make sure we can remove old bits of unused code and also prevent new cases.
One tool we have used for this is Test::Vars. It has some limitations (like it needs to be in a file with a
package declaration) and it has some bugs, but it does find some unused variables as well.
As part of this work, I have released App::perlvars. It’s simple to use. Just install the module and then run it on the
.pm files in your code:
find lib | grep pm$ | xargs perlvars
You can exclude arbitrary variables so that they don’t get flagged. See the documentation for more info on that.
If you use Perl::Critic, I highly encourage you to look at Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter as well. This may be an even better solution, or you may find that using both tools gives you the maximum benefit. To start with, you can try something like this:
perlcritic --single-policy Variables::ProhibitUnusedVarsStricter lib
Added to this, you can also use Test::Vars directly as part of your test suite.
If you’re a tidyall user, I encourage you
to look at
While you’re at it, enable the
.perlcriticrc and you’ll have maximum unused variable checks.
This leaves you with plenty of options for code quality checks in general and for finding unused variables in particular.