|P is for Practical|
(tye)Re: meaning of /o in regexesby tye (Cardinal)
|on Dec 07, 2002 at 06:01 UTC||Need Help??|
First, I'll apologize. I haven't read every node in this thread in detail. But I think there are a few key points about /o that should be brought up that I didn't notice being mentioned.
1) Don't use /o anymore. If you aren't into the subtle points, then just remember that. /o used to provide a rather significant speed improvement in certain rare situtations. Several things have changed related to this. One is that now it provides only a minor speed improvement.
What hasn't changed is that /o can easily lead to bugs in your code. It means that $y =~ /$x/ no longer matches $y against $x but instead matches $y against whatever was in $x the first time the code got executed. This can be quite confusing and it is easy to get this wrong.
2) If you really want the minor speed improvement that /o could still give you, then you should use qr// instead. That is, replace:
or, more likely, where you now set $x to be the one value you want to use, instead set $re= qr/$x/ in that place (sorry, the second example looks a bit complicated, but that is mostly do to with the contrived nature of the example -- in reality, using qr// in place of //o likely makes your code easier to understand).
Using qr// is even better because you get the speed improvement of being able to use $re 10,000 times without recompiling and yet you can update your program such that you do $re= qr/$x/ a second time and then use $re 10,000 more times with only the one extra compilation. qr// is every bit as fast as //o, but it is much more flexible and much less error prone.
3) qr//o is identical to qr//. In fact, japhy (I believe) ran off one day to patch Perl such that qr//o would no longer be supported (since supporting it just leads to confusion as you have now seen). I'm curious to see what happened to that patch, but unfortunately now is not the time to me to go digging for that.
More rambling details...
It used to be that saying /$x/ would cause the regex to be recompiled every single time it was executed. Now, /$x/ is only recompiled if $x has changed since the last time it was recompiled.
qr// gets compiled every time the qr// part is executed, even if you say qr//o. If you really only want it to compile once, then you should only execute the qr// once. (qr//o really should be an error.) BTW, I tested this assertion in Perl 5.006.- tye