Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ChrisDolan (2855)

ChrisDolan
  (email not shown publicly)
http://www.chrisdolan.net/

Journal of ChrisDolan (2855)

Friday September 21, 2007
01:48 AM

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

[ #34505 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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))

     

    • 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.

      • 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?
        • 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?

          • 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.