On Tue Apr 05 13:29:47 2011, EDAVIS wrote:
Show quoted text> Thanks for your recent work on ProhibitUnusedCapture. Unfortunately
> there is another kind of false positive to report. 'perlcritic -1' on
> the following code wrongly reports that the captured value is not being
> used.
>
> #!/usr/bin/perl
> # $Id:$
> use warnings;
> use strict;
> use Carp qw(croak);
> our $VERSION = 1;
>
> sub foo {
> my $x = shift;
> return length $x;
> }
> $_ = 'aa';
> my @x;
> foreach (/(a+)/gsxm) {
> push @x, foo($1);
> }
> print $x[0] or croak;
It looks to me like this may be technically a bug in ProhibitUnusedCapture.
But I wonder why you are using 'foreach' rather than 'while'. The
problem with 'foreach' is that except for special cases the list is
constructed before the block is ever entered. This means that the $1 in
the body of the expression contains the _last_ match on _all_
iterations, which makes the example look very much like a bug.
The attached rt67256a.pl illustrates the difference between the behavior
of 'while' and 'foreach'.