On Fri Apr 22 21:13:04 2011, rtaylor wrote:
Show quoted text> On Thu Apr 21 14:49:33 2011, WYANT wrote:
> > On Thu Apr 21 06:08:45 2011, rtaylor wrote:
> > > The code snippet below violates
> > > RegularExpressions::ProhibitUnusedCapture. PerlTidy should understand
> > > named captures and possibly even encourage their use over unnamed
> > > (position) based captures.
> > >
> > > Capture via brackets (?<digit>\d+) has the same report.
> > >
> > >
> > > #!/usr/bin/env perl
> > >
> > > use v5.10.0;
> > > use strict;
> > > use warnings;
> > >
> > > if ( 'abc12def' =~ m/(?'digit'\d+)/xmisg ) {
> > > say $+{digit};
> > > }
> >
> > Perl::Critic does understand named captures, and your code would still
> > be flagged if you used numbered captures.
> >
> > What is causing the policy to complain is the presence of /g. What is it
> > accomplishing here? Will your code work without it?
>
> Indeed. I can remove the /g and everything works okay. I had thought /g
> did something else (multiple matching versus single or first match)
> which is what it does in PostgreSQL.
>
> Perhaps the ProhibitUnusedCapture could have a "Remember, /g also
> creates a capture" type note.
>
> Thank-you for pointing me in the right direction.
I don't know what /g does in PostgreSQL. In Perl it creates a multiple
match, but you only get one match per iteration. So the code you exhibit
does the same thing with and without the /g. If you really expected to
get and process multiple matches, you would do something like
while ( 'ab12cd34ef56' =~ m/ (?'digits' \d+ ) /sxmg ) {
say $+{digits};
}
Yes, there are some corner cases related to yours that this policy flags
falsely, but the policy is already fairly complex, and I hesitate to
make it more complex before the need is demonstrated.
As a side note, the /i probably does nothing in your particular regular
expression, since it matches no letters.