in reply to Re^2: A variable name in a regex comment triggers a warning
in thread A variable name in a regex comment triggers a warning

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}

Replies are listed 'Best First'.
Re^4: A variable name in a regex comment triggers a warning
by johngg (Canon) on Dec 25, 2006 at 01:30 UTC
    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

Re^4: A variable name in a regex comment triggers a warning
by johngg (Canon) on Dec 25, 2006 at 13:25 UTC
    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