in reply to Re^2: Precompiling qr/.../o question
in thread Precompiling qr/.../o question

In your two additional examples (where you put $_ into the regex's) you are defeating the purpose of the 'compiled' regex. The compile option, I thought, was specifically for cases where the regex itself does *not* change.

I thought the advantage of using the qr() construct was so that the compiler would look at the regex and do the right thing...i.e., compile the regex once if it does not change, otherwise recompile every usage when it *does* change. I'm not sure what the compiler does when you tell it to compile a regex (using the /o option specifier) that is not essentially static...in fact, I vaguely recall reading that it results in unpredictable regex compiler behavior. Is that the cause of the ...different behavior... that is reported in this thread? My recollection of the compiler behavior may just be a bad memory...since I usually work hard to ensure that the regex is really static and can safely expect to compile it only onc.

I don't consider myself to be more than a novice with regex's...I'm gaining more and more experience with them and starting to drift towards that dangerous mental construct of seeing every problem as a regex waiting to birth itself. So all of the other good advice and commentary is from much more accomplished and knowledgable monks.

ack Albuquerque, NM

Replies are listed 'Best First'.
Re^4: Precompiling qr/.../o question
by mr_mischief (Monsignor) on Apr 04, 2008 at 17:31 UTC
    Your question is one of scope, I think. Setting $foo to a compiled regex once per loop, then using it in many matches and/or substitutions later in the loop is one thing. Setting it to not recompile even when otherwise told to do so is another. We could discuss at great length the merits and faults with having those two options, but precompiling the regex once per loop iteration for a loop with two dozen matches using that regex I think still makes sense.

      I didn't realize that the compiler was re-invoked after every loop. I thought the compilation takes place before the entire script is executed (or, I guess more correctly, I should say 'interpreted'). I didn't realize that the decision to compile the regex is done on every pass through the loop.

      Certainly, with the regex compilation decision being invoked on every pass throught the loop, I would absolutely agree that it makes sense to, as you said "...precompiling the regex once per loop iteration for a loop with two dozen matches using that regex I think still makes sense...". That would, as you noted, be much more efficient than having to re-compile on every instance of regex throughout the loop...especially if you had many instances in the loop of using that regex.

      Thanks. I really appreciate your helping me dig deeper into and learn more about the magic of regex's.

      ack Albuquerque, NM

        I didn't realize that the compiler was re-invoked after every loop

        It's not. Looping has nothing to do with it, for starters.

        The regexp compiler is invoked every time a qr//, m// or s/// operator is invoked (and more?), if the regexp has changed since the last time that match or substitute operator has been invoked.

        For example, notice there's no "Compiling" message on the third pass, no matter whether qr// or m// is used.

        >perl -Mre=debug -e"/$_/ for qw( foo bar bar foo )" 2>&1 | find "Compi +ling" Compiling REx `foo' Compiling REx `bar' Compiling REx `foo' >perl -Mre=debug -e"qr/$_/ for qw( foo bar bar foo )" 2>&1 | find "Com +piling" Compiling REx `foo' Compiling REx `bar' Compiling REx `foo'

        A regexp compiled with qr// won't get recompiled when it's included in m// or s/// if it the regexp operand consist entirely of the qr// regexp. However, it might be recompiled when interpolated into another regexp.

        >perl -Mre=debug -e"$re=qr/foo/; /$re/" 2>&1 | find "Compiling" Compiling REx `foo' >perl -Mre=debug -e"$re=qr/foo/; /a$re/" 2>&1 | find "Compiling" Compiling REx `foo' Compiling REx `a(?-xism:foo)'