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


In reply to Re: Detecting 'our $foo => 1' mistake? by haukex
in thread Detecting 'our $foo => 1' mistake? by perlancar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.