develooper Front page | perl.perl5.porters | Postings from January 2014

[perl #121070] (?{...}) not executed in many regexes that do subpattern recursion

Thread Next
Sam S .
January 24, 2014 06:31
[perl #121070] (?{...}) not executed in many regexes that do subpattern recursion
Message ID:
# New Ticket Created by  Sam S. 
# Please include the string:  [perl #121070]
# in the subject line of all future correspondence about this issue. 
# <URL: >

I've been experimenting with using embedded (?{...}) blocks in regexes that also make use of the (?&NAME) "Recurse to a named subpattern" feature, and found that for many such regexes, the embedded code blocks fail to take effect when they should.

Here's a simple test case that demonstrates the bug:

  use Data::Dumper;
  "abcd" =~ /(?&Char) (?{ 42 })
                 (?<Char> . )
  print Dumper $&, $^R;

  $VAR1 = 'a';
  $VAR2 = undef;

Expected output: 42 instead of undef

Curiously, the above test case suddenly *does* work when a superfluous {1,1} is added to the regex like so:

  "abcd" =~ /(?&Char){1,1} (?{ 42 })
                 (?<Char> . )

The output is now:
  $VAR1 = 'a';
  $VAR2 = 42;

This is just one example of how fragile & unpredictable the combination of these two regexp features seems to be; Many cases don't work, although some do.

PS: I'm using Perl 5.18.2 on Arch Linux, installed using the official distro-provided packages. Output of "perlbug -d" is attached.
Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About