Dismas has asked for the wisdom of the Perl Monks concerning the following question:

Hello, O wise ones. If I've understood my reading, the addition of the one-time-compile modifier, "//o" to my regexps saves time only if the pattern inside the regexp is a variable, e.g.:

$pattern = "something"; if ($foo =~ /$pattern/o) { #stuff }
Is my understanding correct, or does the "o" still help with an actual *expression* inside, e.g.:

if ($foo =~ /^dis_isa_pattern$/o) { # stuff }
Many thanks,
Deane

Replies are listed 'Best First'.
Re: //o of any help?
by diotalevi (Canon) on Sep 22, 2004 at 15:24 UTC

    No. It doesn't.

    You also slightly misunderstood the first case. It saves time by permanently hardwiring the value of $pattern when the match is attempted. If you change $pattern later, the regex is still hardwired to the original. See /o is dead, long live qr//! for lots of gory details.

Re: /o of any help?
by duff (Parson) on Sep 22, 2004 at 16:00 UTC

    Everytime I see an /o on a RE, it's usually the wrong thing. And it irks me to no end when it's used on REs that have no variables in them. It's a maintenance nightmare waiting to happen.

    Imagine a RE with /o and no variables buried in a module that's part of a much larger application. In the future, some enterprising programmer decides that the RE would be better off with a $variable than what's already there but he forgets to remove the /o (or it may be so far removed from where he added the $variable, that it's not immediately visible (/x is both blessing and curse :)). Everything will work fine as long as the routine that uses that RE is called exactly once per execution of the program. The moment you put it in a loop surprising things may happen. But you may not even notice anything is wrong for a while. And tracking it down to that one little /o modifier could take ages!

    BTW, something like the above actually happened to me several years ago, but I was both the person who created the RE and the future enterprising programmer. :-)

Re: //o of any help?
by davido (Cardinal) on Sep 22, 2004 at 16:11 UTC

    Many see /o as a performance enhancer first, with the side effect of locking in the state of the first variable interpolation. I maintain that locking in the state of the first variable interpolation should be looked upon as the primary effect, and enhanced performance as a distant second side-effect. Chances are good that you don't even really need better performance in the particular segment of code that uses an RE with the /o modifier. Only profiling can tell you for sure, and even that should probably only be used if the entire script isn't meeting its speed performance needs.

    In either case, /o has been mostly replaced by the qr// operator, discussed in perlop. ...and /o remains in the Perl vocabulary, IMO, for backward compatibility more than anything. New code going forward should almost always use qr// instead of /o, if for no other reason that qr// is clearer and easier to maintain.


    Dave