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

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

Thread Next
From:
Sam S .
Date:
January 24, 2014 06:31
Subject:
[perl #121070] (?{...}) not executed in many regexes that do subpattern recursion
Message ID:
rt-4.0.18-24141-1390512480-1767.121070-75-0@perl.org
# New Ticket Created by  Sam S. 
# Please include the string:  [perl #121070]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=121070 >


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 })
             (?(DEFINE)
                 (?<Char> . )
             )/x;
  
  print Dumper $&, $^R;

Output:
  $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 })
             (?(DEFINE)
                 (?<Char> . )
             )/x;

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


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