warnings::unused versus PPI
I had the following in my .bash_aliases file:
alias unused='perlcritic --single-policy ProhibitUnusedVariables'
However, I found a few cases where it didn't work. That's when mithaldu told me about warnings::unused. I installed it locally and ran it on some code which had a case that ProhibitUnusedVariables didn't find:
$ unused $some_module
$some_module source OK
$ perl -Mwarnings::unused -c $some_module 2>&1 | wc -l
34
Whoa! The one area were Perl::Critic modules repeatedly break for me is when I'm dealing with scope issues. It appears to miss where a variable is legitimately declared and used in one sub, but declared and unused in another. It also missed something like the following:
while ( my ($k,$v) = each %hash ) {
my $k = munge($v);
}
And lucky me! The output of warnings::unused fits the format I'm expecting with the VI::Quickfix code I posted, so I just type :cf and :cn to jump to the next unused variable. Bonus: go ahead and delete them: vim's quickfix mode will note the deleted lines and still jump to the correct line.
Try dropping this in your .vimrc:
au! FileType perl :noremap <leader>c
\ :!time perl -Mwarnings::unused -MVi::QuickFix -c %<cr>
With that, every time you hit your leader and 'c', you'll get a quick compile check and a dump of all unused variables. Just hit ':cn" to keep jumping to the next one (or the next error, if you have any).
(Now if I can find some nifty code which finds variables declared outside of the scope they're needed ...)
Can perlcritic not find those cases and should an issue be raised for it?
@Robert: that's correct, Perl::Critic can't find those cases (I should have pointed out that this may be a Critic issue instead of a general PPI one, unless the latter doesn't help you find scope).
I should raise bugs, but I found so many instances of this a couple of hours ago that I was busy fixing code rather than reproducing test cases and filing tickets.
Could you explain why warnings::unused doesn't work for this script
but does for this script
and this one liner
?
Could you explain why warnings::unused doesn't work for this script
use warnings; my $foo;
$ perl -Mwarnings::unused -c foo.pl foo.pl syntax OK
but does for this script
use warnings; { my $foo; }
$ perl -Mwarnings::unused -c foo.pl Unused variable my $foo at foo.pl line 2. foo.pl syntax OK
and this one liner
$ perl -Mwarnings::unused -e 'use warnings; my $foo' Unused variable my $foo at -e line 1.
?
Robert: I've filed a bug with minimal test cases at https://rt.cpan.org/Ticket/Display.html?id=64929
These all fail in the latest version of Perl::Critic.
I've no idea why it doesn't capture that as an issue and I've filed a bug report for it:
https://rt.cpan.org/Ticket/Display.html?id=64930
Hopefully it will be fixed in the next release.
Ovid: I wasn't pointing the finger. I was wondering if that was something it should catch so a bug could be filed against it. I hope you took it in the best way.
Robert: I didn't take it that way, but in rereading my reply, it may have seemed like that. Sorry!
Possible typo? Third paragraph, you refer to "warnings::register" rather than "warnings::unused"...
@rjray: thanks. I fixed that.