in reply to Detecting 'our $foo => 1' mistake?
Hi perlancar,
I whipped up a Perl::Critic policy for you because I think it might also be useful to others... drop the following into, for example, /tmp/testlib/Perl/Critic/Policy/Variables/ProhibitFatCommaInDeclaration.pm
package Perl::Critic::Policy::Variables::ProhibitFatCommaInDeclaration +; use warnings; use strict; use base 'Perl::Critic::Policy'; use Perl::Critic::Utils ':severities'; use List::Util 'first'; our $VERSION = '0.001'; sub supported_parameters { return } sub default_severity { return $SEVERITY_HIGH } sub default_themes { return qw/core bugs/ } sub applies_to { return 'PPI::Statement::Variable' } sub violates { my ($self, $elem) = @_; my $found = first { $_->isa('PPI::Token::Operator') } $elem->child +ren; if ($found && $found->content eq '=>') { return $self->violation('Fat comma used in declaration', 'You probably meant "=" instead of "=>"', $found); } return; } 1;
Then:
$ cat testpolicy.pl #!/usr/bin/env perl use warnings; use strict; my $foo => 'bar'; our $foo => 1; our $foo => { a=>'blah', b=>'blah' }; our $foo = { a=>'blah', b=>'blah' }; my %foo = ( a=>1, b=>2 ); $ PERL5LIB=/tmp/testlib perlcritic -4 testpolicy.pl Fat comma used in declaration at line 5, column 9. You probably meant + "=" instead of "=>". (Severity: 4) Fat comma used in declaration at line 6, column 10. You probably mean +t "=" instead of "=>". (Severity: 4) Fat comma used in declaration at line 7, column 10. You probably mean +t "=" instead of "=>". (Severity: 4)
Really just a quick one for now, so I haven't done any further testing except the above, but I hope it's a start :-)
Update: You could perhaps monkey-patch this into your Perl::Critic installation by copying ProhibitFatCommaInDeclaration.pm into the same directory as the one shown when you say perldoc -l Perl::Critic::Policy::Variables::ProhibitLocalVars (untested). Or perhaps you've already got a local::lib setup. Of course, this could be turned into a real module distro too, but I haven't gotten that far :-)
Hope this helps,
-- Hauke D
|
|---|