in reply to A variable name in a regex comment triggers a warning

I actually would have expected it to interpolate as you describe :)

If you have a newline in $deCommafied, there certainly will be an effect; anything after the newline isn't part of the comment:

$ perl -wle'use re "debug"; $x = "foo\nbar"; qr/(?x)hullo #there $x/' Compiling REx `(?x)hullo #there foo bar' size 4 Got 36 bytes for offset annotations. first at 1 1: EXACT <hullobar>(4) 4: END(0) anchored `hullobar' at 0 (checking anchored isall) minlen 8 Offsets: [4] 5[20] 0[0] 0[0] 25[0] Freeing REx: `"(?x)hullo #there foo)\nbar"'

FWIW, using qr{ ... }x; instead of qr{(?x) ... }; seems to do what you expect. But I'd try to avoid relying on that. Also watch out for having your end-quote character ('}' in your example) in the comment.

Replies are listed 'Best First'.
Re^2: A variable name in a regex comment triggers a warning
by johngg (Canon) on Dec 24, 2006 at 23:10 UTC
    Thank you for your reply. It raises a couple of questions in my mind. Firstly, why the difference between qr{(?x)...} and qr{...}x? I ran the following and, yes, it looks like there's no interpolation with qr{...}x

    $ perl -wle'use re "debug"; $x = "foo\nbar"; qr/hullo #there $x/x' Compiling REx `hullo #there $x' size 4 Got 36 bytes for offset annotations. first at 1 1: EXACT <hullo>(4) 4: END(0) anchored `hullo' at 0 (checking anchored isall) minlen 5 Offsets: [4] 1[15] 0[0] 0[0] 16[0] Name "main::x" used only once: possible typo at -e line 1. Freeing REx: `"hullo #there $x"'

    (Although I can't say that I really understand most of the re 'debug' output :-)

    Secondly, why would you want interpolation inside comments anyway? I can see that interpolating a newline into the comment would stop the comment after the newline but to my mind doing the interpolation in the first place is not what you might reasonably expect.

    BTW, initialising $deCommafied before the qr{...} also silences the warning for reasons I now understand.

    Cheers,

    JohnGG

      Firstly, why the difference between qr{(?x)...} and qr{...}x?

      I guess that has to do with the stages of regex compilation. After the closing token for qr{ is found, the outer flags are checked. If there's an x modifier for the whole regex, comments will be stripped. Then variable interpolation is done. After that, the pattern is compiled, and it's at this stage the internal starting (?x) is seen. Alas, variables are already interpolated, which in your case generated a warning. Comments are then stripped to the end of the pattern (or the next (?-x)modifier).

      Does that make sense? If what I describe is the case, it would answer your second question too.

      -shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Thank you for the explanation, which was very clear and made perfect sense. I think I now understand what is going on. Is variable interpolation suppressed inside a (?#...) construct? That would explain why it stopped the warning. Let's see

        $ perl -wle'use re "debug"; $x = "foo\nbar"; qr/(?x)hullo (?#there $x) +/' Compiling REx `(?x)hullo (?#there $x)' size 4 Got 36 bytes for offset annotations. first at 1 1: EXACT <hullo>(4) 4: END(0) anchored `hullo' at 0 (checking anchored isall) minlen 5 Offsets: [4] 5[6] 0[0] 0[0] 23[0] Name "main::x" used only once: possible typo at -e line 1. Freeing REx: `"(?x)hullo (?#there $x)"'

        Looks like it is. Thanks again for the explanation. I know now what to avoid.

        Cheers,

        JohnGG

        Thinking about this further (yes, I know I should be concentrating on festivities, my wife has already told me) I am still a bit puzzled. Following the stages as you describe, there is no outer x flag so comments are not stripped once the closing token is found. Then variable interpolation is done before the pattern is compiled so nothing should yet be known about the semantics of the pattern. However, it seems that the compiler is aware somehow that variables don't interpolate inside (?# ... ) constructs, as seen here whereas it isn't with extended syntax comments.

        It would appear that some form of pre-compilation parsing is happening so that it is aware of comment blocks before variable interpolation. I wonder if it would also be able to take note of (?x) constructs. The problem would be separating the x from the possible ism etc.

        Cheers,

        JohnGG