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

[perl #121299] Inconsistent behavior with backreferences nested inside subpattern references

From:
Nathan Trapuzzano
Date:
February 22, 2014 01:56
Subject:
[perl #121299] Inconsistent behavior with backreferences nested inside subpattern references
Message ID:
rt-4.0.18-7497-1393034181-1994.121299-75-0@perl.org
# New Ticket Created by  Nathan Trapuzzano 
# Please include the string:  [perl #121299]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=121299 >


This bug report corresponds to the discussion at:
http://www.nntp.perl.org/group/perl.perl5.porters/2014/02/msg212894.html

Perl (5.16.3 and 5.18.1 at least) doesn't always handle backreferences
inside subpattern calls correctly.  For example,

  say 'fofof' =~ /(.)((o)\1)(?2)/ ? 'true' : 'false'
  ==> true

  say 'fffffff' =~ /(.)(?2)((\1)(?4)(\1))/ ? 'true' : 'false'
  ==> false

  say 'foffoff' =~ /(.)(?2)((.)(?4)(\1))/ ? 'true' : 'false'
  ==> false

"use re 'debug'" shows that the second and third examples fail at the
first '\1' encounter, as though the first capture were being reset upon
entering the sub call.  However, in the first example, the capture is
cearly retained in the '(?2)' call.

A similar (same?) problem occurs with forward or self-referential
backreferences:

  print 'abcb' =~ /^(.\2?)(.)(?1)$/ ? "true\n" : "false\n"
  => false

  print 'abcb' =~ /^(.(?{ printf "%d, $2\n", pos()})\2?)(.)(?1)$/ ? "true\n" : "false\n"
  => 1, 
  => 3, b
  => false

  print 'aba' =~ /^(.\1?)(?1)$/ ? "true\n" : "false\n"
  => false

  print 'aba' =~ /^(.(?{printf "%d, $1\n", pos()})\1?)(?1)$/ ? "true\n" : "false\n"
  => 1, 
  => 2, a
  => false

In second example immediately above, the diagnostics say that we're
about to try to match 'b' at pos 3, which should succeed.  Ditto for the
fourth example.




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