Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Perl-Critic CPAN distribution.

Report information
The Basics
Id: 14787
Status: resolved
Priority: 0/
Queue: Perl-Critic

People
Owner: thaljef [...] cpan.org
Requestors:
Cc: kclark [...] cpan.org
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.07
Fixed in: 0.10



Subject: $1 seen as magic punctuation variable
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.
[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
From: kclark [...] cpan.org
[guest - Fri Sep 30 17:52:17 2005]: I'd like to expand my earlier patch to also allow for the "_" variable (already stat'd file): diff -c ProhibitPunctuationVars.pm.orig ProhibitPunctuationVars.pm *** ProhibitPunctuationVars.pm.orig 2005-09-30 17:49:47.000000000 -0400 --- ProhibitPunctuationVars.pm 2005-10-03 16:21:50.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,32 ---- 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 # start of string ! (?: # non-capturing, for any of... ! [\$\@]_ # $_ or @_ ! | # or ! \$\d+ # $1, $2, ... ! | # or ! _ # already stat'ed file ! ) # end alternation ! \z # end of string ! }x ! } @{$nodes_ref}; return map { Perl::Critic::Violation->new( $desc, $expl, $_->location() ) } @matches; } ky
Date: Mon, 3 Oct 2005 14:42:28 -0700 (PDT)
From: Jeffrey Thalhammer <jeffrey_thalhammer [...] yahoo.com>
Subject: Re: [cpan #14787] $1 seen as magic punctuation variable
To: bug-Perl-Critic [...] rt.cpan.org
RT-Send-Cc:
Cool, thanks for the tip. For version 0.09, I switched to a hash lookup instead of a regex. I will add the magic '_'. Actually, I don't know how PPI parses that. I'll have to check it out when I get home. -Jeff --- via RT <bug-Perl-Critic@rt.cpan.org> wrote: Show quoted text
> > This message about Perl-Critic was sent to you by > KCLARK <KCLARK@cpan.org> via rt.cpan.org > > Full context and any attached attachments can be > found at: > <URL: > https://rt.cpan.org/Ticket/Display.html?id=14787 > > > [guest - Fri Sep 30 17:52:17 2005]: > > I'd like to expand my earlier patch to also allow > for the "_" variable > (already stat'd file): > > diff -c ProhibitPunctuationVars.pm.orig > ProhibitPunctuationVars.pm > *** ProhibitPunctuationVars.pm.orig 2005-09-30 > 17:49:47.000000000 -0400 > --- ProhibitPunctuationVars.pm 2005-10-03 > 16:21:50.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,32 ---- > 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 # start of string > ! (?: # non-capturing, for any > of... > ! [\$\@]_ # $_ or @_ > ! | # or > ! \$\d+ # $1, $2, ... > ! | # or > ! _ # already stat'ed file > ! ) # end alternation > ! \z # end of string > ! }x > ! } @{$nodes_ref}; > return map { Perl::Critic::Violation->new( > $desc, $expl, > $_->location() ) } > @matches; > } > > ky >
Show quoted text
__________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com
[jeffrey_thalhammer@yahoo.com - Mon Oct 3 17:42:56 2005]: Show quoted text
> Cool, thanks for the tip. For version 0.09, I > switched to a hash lookup instead of a regex. I will > add the magic '_'. Actually, I don't know how PPI > parses that. I'll have to check it out when I get > home.
Off the top of my head, I think it should do it properly and consider it a mafic variable. I know I've encountered it before. If not, it definitely should.
Works as advertised. PPI does lex '_' into a magic variable, although it is not listed in the documentation. -Jeff [ADAMK - Wed Oct 5 01:00:20 2005]: Show quoted text
> [jeffrey_thalhammer@yahoo.com - Mon Oct 3 17:42:56 2005]: >
> > Cool, thanks for the tip. For version 0.09, I > > switched to a hash lookup instead of a regex. I will > > add the magic '_'. Actually, I don't know how PPI > > parses that. I'll have to check it out when I get > > home.
> > Off the top of my head, I think it should do it properly and consider > it a mafic variable. I know I've encountered it before. If not, it > definitely should.