in reply to Re: Re: •Re: RegEx re-compilation
in thread RegEx re-compilation

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.

Replies are listed 'Best First'.
Re: &bull;Re: Re: Re: &bull;Re: RegEx re-compilation
by Melly (Chaplain) on Nov 17, 2003 at 15:06 UTC

    Aha!

    Many thanks - of course from the point of view of of a non-hacker this raises as many questions as it answers (well, 2:1 in this case ;)

    Is perl just checking whether the value of the vars in the regex have changed, or the regex opcodes?

    In either case (but particularily the latter), isn't there some overhead involved in checking whether recompilation is needed? Indeed, if an opcode comparison is taking place, won't that take just as long as recompiling (since it will presumably have to recompile the "new" expression in order to compare it with the old)?

    I may be wildly off-beam here, but it seems a similiar situation to:

    $foo = 'hello world' unless $foo eq 'hello world';

    being a slightly pointless check, and better replaced with:

    $foo = 'hello world';

    Of course, I'm probably wrong about that as well.... which I could well be if assignment is more time consuming than comparison...

    Tom Melly, tom@tomandlu.co.uk