Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

(tye)Re: meaning of /o in regexes

by tye (Cardinal)
on Dec 07, 2002 at 06:01 UTC ( #218222=note: print w/ replies, xml ) Need Help??


in reply to meaning of /o in regexes

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:

sub foo { $y =~ /$x/o; }
with
BEGIN { my $re; sub foo { $re= qr/$x/ unless $re; $y =~ $re; } }
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


Comment on (tye)Re: meaning of /o in regexes
Select or Download Code
Re: (tye)Re: meaning of /o in regexes
by CountZero (Bishop) on Dec 08, 2002 at 19:14 UTC

    And of course if you happen to run your regex in a Perl script in an Apache Webserver with mod-perl your regex gets instantiated to whatever value the first user put into it as your script gets compiled only once for an untold number of requests.

    It can make for a lot of interesting bugs!

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://218222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-07-11 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (236 votes), past polls