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: 36016
Status: open
Priority: 0/
Queue: Perl-Critic

People
Owner: Nobody in particular
Requestors: amir.aharoni [...] mail.huji.ac.il
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.082
Fixed in: (no value)



Subject: Prohibit numbered captures in Perl 5.10
Perl 5.10 offers a new way of using string captured in regular expressions - instead of using $1, $2 etc., you can give them names and refer to them using %+ or its English alias %LAST_PAREN_MATCH (thanks to Elliot Shank for clarifying that last bit in bug #35969). This improves code readability, much like use English, so when running with a Perl version that has it (5.010 or higher) P::C should suggest using it instead of $1. An example is attached. Suggested new warnings: * 'Anonymous capture used in regular expression' - when using '(expr)' instead of '(?<capture_name>expr)' in the regex. * 'Numbered capture used instead of named captures' - when using any of the $<digit> variables. * 'Numbered backreference used in regular expression' - when using \1 instead of \k<capture_name> Of course, feel free to suggest further improvements and better wording for the warnings. (English is not my native language.) I use P::C a lot, but i am far from being an expert in its intricacies. This suggestion probably affects other policies, but i'm not sure which ones.
Subject: named_captures.pl
#!/usr/bin/perl use 5.010; use strict; use warnings; use English qw(-no_match_vars); our $VERSION = 0.1; my $string = 'Perl is a language for getting your job done.'; # The next line should trigger the warning: # 'Anonymous capture used in regular expression' if ($string =~ /\b(\w)\b/xms) { # The next line should trigger the warning: # 'Numbered capture used instead of named captures' say "the string has the one-char word $1"; } # This block shouldn't trigger any warnings if ($string =~ /\b(?<one_char_word>\w)\b/xms) { say "the string has the one-char word $LAST_PAREN_MATCH{one_char_word}"; } # The next should trigger three warnings: # 'Anonymous capture used in regular expression' # 'Numbered backreference used in regular expression' if ($string =~ /(.)\1/xms) { # The next line should trigger the warning: # 'Numbered captures used instead of named captures' say "the string has the repeating char $1"; } # This block shouldn't trigger any warnings. # (?<repeating_char>) and (?'repeating_char'.) are supposed to be equivalent. if ($string =~ /(?'repeating_char'.)\k{repeating_char}/xms) { say "the string has the repeating char $LAST_PAREN_MATCH{repeating_char}"; } exit; __END__
Subject: Re: [rt.cpan.org #36016] Prohibit numbered captures in Perl 5.10
Date: Mon, 19 May 2008 16:17:02 -0500
To: bug-Perl-Critic [...] rt.cpan.org
From: Elliot Shank <perl [...] galumph.com>
Amir E. Aharoni via RT wrote: Show quoted text
> Perl 5.10 offers a new way of using string captured in regular > expressions - instead of using $1, $2 etc., you can give them names and > refer to them using %+ or its English alias %LAST_PAREN_MATCH (thanks to > Elliot Shank for clarifying that last bit in bug #35969). > > This improves code readability, much like use English, so when running > with a Perl version that has it (5.010 or higher) P::C should suggest > using it instead of $1.
These sound interesting. However, I can foresee some howls of protest with some of them. Perhaps candidates for P::C::More?
From: amir.aharoni [...] gmail.com
On Mon May 19 17:17:14 2008, clonezone wrote: Show quoted text
> These sound interesting. However, I can foresee some howls of protest > with some of them. Perhaps candidates for P::C::More?
Put it in the brutal severity level. (I use it most of the time anyway :) I took a look at P::C::More and its siblings, and i think that it should go to the main P::C. It is as relevant as Variables::ProhibitPunctuationVars. $1 is very common, but so are $@, $!, $$ and all the rest of perlvar. And besides, i always viewed PBP and P::C as great ways to learn features of Perl that i don't know, so this may be a way to teach people about the nice new features of 5.10. For those who won't like it there's always .perlcriticrc.
Subject: Re: [rt.cpan.org #36016] Prohibit numbered captures in Perl 5.10
Date: Mon, 19 May 2008 17:58:58 -0500
To: bug-Perl-Critic [...] rt.cpan.org
From: Elliot Shank <perl [...] galumph.com>
Amir E. Aharoni via RT wrote: Show quoted text
> On Mon May 19 17:17:14 2008, clonezone wrote:
>> These sound interesting. However, I can foresee some howls of protest >> with some of them. Perhaps candidates for P::C::More?
> > Put it in the brutal severity level. (I use it most of the time anyway :)
Ah, you're the diplomatic type, I see. :]