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

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

Thread Previous | Thread Next
From:
Karl Williamson
Date:
November 27, 2011 06:53
Subject:
Re: [perl #98352] Postponed code subregex *can* see capture groups
Message ID:
4ED24ED6.9080806@khwilliamson.com
On 11/26/2011 06:06 PM, Father Chrysostomos via RT wrote:
> On Sat Sep 03 09:42:50 2011, sprout wrote:
>> 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(The code block itself can use C<$1>, etc.,
  > to refer to the enclosing patterns capture groups.)
>>> outside itself.  Either the documentation or the code should be changed.
>>
>> Maybe the documentation should be clarified. Do you have any suggestions?
>>
>
> I hope I have clarified this sufficiently with commit b271183.
>

I'm afraid I don't understand this "(The code block itself can use 
C<$1>, etc., to refer to the enclosing patterns capture groups.)"

Should "patterns" be "pattern's", or is something else intended?

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