in reply to RegEx re-compilation

It's not implied. It's irrelevant. Since /o controls the treatment of variables, and there are no variables, it can neither be implied to be present or absent. The question has no merit.

It's like saying "do I still get a free second helping of my entree with this coupon when all I'm having is dessert?" People turn their heads sideways when you say that. {grin}

Is it helpful? No. Is it harmful? No, except that including that switch on a regex without variables might make me question the rest of your Perl understanding in a code review.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: RegEx re-compilation
by duff (Parson) on Nov 14, 2003 at 17:02 UTC
    Is it helpful? No. Is it harmful? No, except that including that switch on a regex without variables might make me question the rest of your Perl understanding in a code review.

    I'd say that it can be harmful. Given the dynamic nature of code, an /o on a RE could cause some hard-to-find bugs if the RE were modified to use a variable but the /o not removed. In fact, I'd advocate that /o on a RE without variables should illicit a warning when -w is in effect.

Re: •Re: RegEx re-compilation
by melora (Scribe) on Nov 14, 2003 at 17:21 UTC
    Sounds like it's maybe harmful to the guy that comes along three years later to maintain the code. In my experience, something that's not needed and can cause confusion is better left out.
Re: •Re: RegEx re-compilation
by habit_forming (Monk) on Nov 14, 2003 at 16:32 UTC
    Thank you for the explanation. Apparently, I was very confused.

    --habit

      Funnily enough I came to the site to ask the same thing, and there was your question sitting at the top...

      IMHO RS was a bit harsh on you (and, by inference, me) - for a general user (i.e. one who hasn't had to write their own regex parser), it is by no means obvious that a regex is only 'compiled' when it contains variables.

      IMHO the problem is that 'compiled' is the wrong word in this context - compiling is something that is done to all code, not just variables. 'Expanded' might be a clearer term, but waddaiknow?

      Tom Melly, tom@tomandlu.co.uk
        No, all regex get compiled into regex opcodes. If they contain no variables, they are compiled once at Perl compile-time. If they contain variables, then the /o flag controls whether the compilation happens the first time it is seen at runtime, or every time it is seen at runtime. But the compilation always happens. It must happen... you can't interpret a regex "directly".

        Also note that some modern innovations make /o somewhat less nasty. The regex engine maintains a one-level cache, so if the regex to be compiled hasn't changed and was the most recent one compiled, the cached compilation is reused. That means you don't need /o for this:

        my @winners = grep /prefix$part/, @list;
        In older Perls, that regex would have been recompiled for each element of @list. Not so, now. Even works for stuff like this:
        while (<>) { next unless /prefix$part/; ... }
        as long as the rest of the loop doesn't use any other recompile-on-demand regex. But the moment it does, you've got two regexes both being compiled on each iteration, and you get molasses at that point.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        Normally REs are compiled each time just before they are executed (modulo optimizations where perl has determined that it doesn't need to recompile the RE). The /o flag means only compile the RE once, no matter what. And compiling it once only has semantic effect when there are variable substitutions to be performed before compilation.

        Perhaps Randal was harsh, but that's okay if the message got through: don't use /o until you understand what it means (Or as I like to shorten it to: don't use /o :-)