How do you think m/[#]/x should be parsed?

ChrisDolan on 2007-09-21T06:48:58

What would you expect this to emit?

  my $qr = qr/f/;
  print qr/[#]$qr/x;


And given the actual (surprising?) result of that, what would you expect this to emit?
  my $qr = qr/(f)/;
  print "#f" =~ m/[#](f)/x;
  print "#f" =~ m/[#]$qr/x;


I get the same results for Perl 5.8.6, 5.8.8 and 5.9.5.


seems to be a bug

steph on 2007-09-21T07:43:36

looks like a bug to me. somehow the interpolation stops at the first # found.




  % steph@ape (/home/stephan) %
  % cat t1.px
  #! /usr/bin/perl

  my $qr = qr/f/;
  print qr/$qr[#]$qr/x;

  my $qr2 = qr/f/;
  print qr/[b]$qr2/x;

  % steph@ape (/home/stephan) %
  % perl -w t1.px
  (?x-ism:(?-xism:f)[#]$qr)(?x-ism:[b](?-xism:f))

 

Re:seems to be a bug

Aristotle on 2007-09-21T13:55:47

No, it’s not really a bug. An unescaped hashmark always starts a comment in patterns where /x applies. If the hashmark were backslash, it would produce the result that Chris probably expected.

There is a definite argument to make that it’s counterintuitive, though. Whitespace in character classes is significant even under /x, so one would expect comments (which are usually thought of as a special kind of ignorable whitespace) would not be allowed in the middle of a character class.

Re:seems to be a bug

ChrisDolan on 2007-09-22T01:12:45

If it's not a bug, then why does the following print "f" and not "1"?

print "#f" =~ m/[#](f)/x;
Or more to the point, if the "#" is a comment character, why is it not a syntax error for an unclosed "[]" group?

Re:seems to be a bug

Aristotle on 2007-09-22T07:13:03

Ack. Yeah, definitely a bug then. Seems different parts of the regex parser have different ideas of when they’re inside a comment, or something like that.

Did you write to p5p?

Re:seems to be a bug

ChrisDolan on 2007-09-22T11:31:07

My first attempt, before posting this journal entry, vanished into the ether (in retrospect, probably because my ISP blocks port 22). I just re-submitted. Thanks for reminding me.