develooper Front page | perl.perl5.porters | Postings from September 2011

[perl #98352] Postponed code subregex *can* see capture groups

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
September 3, 2011 09:42
Subject:
[perl #98352] Postponed code subregex *can* see capture groups
Message ID:
rt-3.6.HEAD-31297-1315068169-1693.98352-15-0@perl.org
On Sat Sep 03 07:47:42 2011, peter@peterscott.com wrote:
> perlre says:
> 
>         "(??{ code })"
> [...]
>             The result of evaluation is considered a regular
expression and
>             matched as if it were inserted instead of this construct.
 Note
>             that this means that the contents of capture groups defined 
> inside
>             an eval'ed pattern are not available outside of the
pattern, and
>             vice versa, there is no way for the inner pattern to refer
to a
>             capture group defined outside.  Thus,
> 
>                 ('a' x 100)=~/(??{'(.)' x 100})/
> 
>             will match, it will not set $1.
> 
> The last assertion is true, however, the one before it appears not to be:
> 
> % perl -E '"abc" =~ /(.)(??{say "\$1=$1"; "(.)"})/ and say "\$2=$2"'
> $1=a
> $2=
> 
> In other words, the subexpression

It says ‘inner pattern’, not subexpression.  That appears to be true:

$ perl -E '"aa" =~ /^(.)(??{say "\$1=$1"; q"(\1)"})/ ? say "\$2=$2" :
say "fail"'
$1=a
fail

> *can* access capture groups defined
> outside itself.  Either the documentation or the code should be changed.

Maybe the documentation should be clarified. Do you have any suggestions?



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About