[THALJEF - Wed Sep 28 01:00:18 2005]:
Show quoted text> When I run perlcritic to analyse this code:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $file = '/home/grant/data.txt';
>
> if($file =~ m{/home/(.*?)/}) {
> my($user) = ($1);
> print "User is: $user\n";
> }
>
> I get this report:
>
> Magic punctuation variable used at line 9, column 16. See page 79 of
> PBP.
>
> It seems to think that $1 is a 'magic punctuation' variable.
>
> Oddly, if I simplify the code to this:
>
> if($file =~ m{/home/(.*?)/}) {
> print "User is: $1\n";
> }
>
> then I don't get an error. The PBP reccomendation on capture
> variables
> (page 254) actually recommends the first approach.
This patch seems to fix the problem:
$ diff -c ProhibitPunctuationVars.pm.orig ProhibitPunctuationVars.pm
*** ProhibitPunctuationVars.pm.orig 2005-09-30 17:49:47.000000000 -0400
--- ProhibitPunctuationVars.pm 2005-09-30 17:51:34.000000000 -0400
***************
*** 13,20 ****
my $expl = [79];
my $desc = q{Magic punctuation variable used};
my $nodes_ref = $doc->find('PPI::Token::Magic') || return;
! #Filter out $_ and @_. These are common enough to allow.
! my @matches = grep { $_ !~ m{\A [\$\@]_ \z}x } @{$nodes_ref};
return map { Perl::Critic::Violation->new( $desc, $expl,
$_->location() ) }
@matches;
}
--- 13,20 ----
my $expl = [79];
my $desc = q{Magic punctuation variable used};
my $nodes_ref = $doc->find('PPI::Token::Magic') || return;
! # Filter out $_, @_ and $1-matches. These are common enough to allow.
! my @matches = grep { $_ !~ m{\A (?:[\$\@]_|\$\d+) \z}x }
@{$nodes_ref};
return map { Perl::Critic::Violation->new( $desc, $expl,
$_->location() ) }
@matches;
}
ky