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
| [reply] |
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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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 :-)
| [reply] |