develooper Front page | perl.perl5.porters | Postings from May 2012

What should qr/$foo\80/ do?

Thread Next
From:
Karl Williamson
Date:
May 31, 2012 08:14
Subject:
What should qr/$foo\80/ do?
Message ID:
4FC78AD4.6020703@khwilliamson.com
The following code:
  my $count = 80;
  my $foo = "(.)" x $count; qr/$foo\80/

generates code to match $count characters, and, then the eightieth (as 
long as  $count >= 80).  But what should happen for $count = 79?  Does 
anyone disagree that it should raise the fatal error "Reference to 
nonexistent group in regex"?, which is what happens when we are looking 
for 8 backreferences but only 7 are present?

$ blead -E 'my $count = 7; my $foo = "(.)" x $count; qr/$foo\8/'
Reference to nonexistent group in regex; marked by <-- HERE in 
m/(.)(.)(.)(.)(.)(.)(.)\8 <-- HERE / at -e line 1.

If you agree with my claim, then what about $count = 78, ... 3, 2, 1, 0 
?  Shouldn't it raise the same error?

What in fact happens is that it does match the eightieth backreference 
for all $count >= 80.  But when $count is less than 80, it silently 
compiles "\00080" in where the "\80" is; that is, it matches a NUL, then 
the digit 8, then the digit 0.

I claim this is a bug, and have written a small patch to fix it.  But 
given the controversy over previous attempts to fix weird cases like 
this, I thought I had better check it out on the list first.

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