in reply to regex "o" modifier

To add to what diotalevi said, Perl already performs this optimization if your regex does not contain variables which must be interpolated. Perl compiles the regex into an internal format and stashes it to use during the program. When Perl encounters a variable in a regex it can't be sure that the variable will never change and so it compiles it each time the regex is used. If you, as the programmer know that the variables in the regex will never change, you can use the o option to tell perl that it is okay to compile this regex once and reuse it over and over.

Bye

Replies are listed 'Best First'.
Re: Re: regex "o" modifier
by bart (Canon) on May 07, 2003 at 09:59 UTC
    When Perl encounters a variable in a regex it can't be sure that the variable will never change and so it compiles it each time the regex is used.
    That used to be true, long time ago, but it is no longer so. Even if your regexp contains variables and you don't use /o, perl may still not compile it again... simply because these days, perl contains a check to see if the scalar has changed since last time the regexp got compiled...

    Benchmarking will show you that, in case the scalar doesn't change, not using /o is comparable in speed, or might even be appear slightly faster (difference < 1%) than when using /o — but likely that's just inaccuracies in the benchmark.

    /o does prevent recompiling the regexp, even if the scalar changes. Likely this is not what you want.

    Therefore, use of /o in modern perl is largely obsolete.

      You should attribute the difference between /constant/o and /constant/ to noise. I've checked the source code, followed the execution path with gdb and verified that there is no C code difference between the two. Consider it like comparing the performance benefits between "\40", " " and "\x32".

Re: Re: regex "o" modifier
by Popcorn Dave (Abbot) on May 06, 2003 at 22:59 UTC
    Just a quick question on something here.

    If I had the following:

    my $regex = qr!yes|no!; my $response = "yes"; print "Not maybe" if $response =~ /$regex/o

    Is that a case where the /o would come in to play as that isn't going to change or does the qr compile the regex before the if statement?

    Thanks!

    There is no emoticon for what I'm feeling now.

      nope, you wouldn't benefit from an /o in this case..

      qr quotes and compiles the pattern as a regular expression. To see what it's doing, try this:

      perl -e 'my $re = qr!yes|no!; print "$re\n";' prints: (?-xism:yes|no)

      The result of qr, in this case $re, can be used on it's own, or within another regex.

      my $re = qr!yes|no!; print "yes or no!\n" if "yes" =~ $re; print "yes or no only" if "maybe" !~ $re; print "polite yes or no\n" if "yes thankyou" =~ /$re thankyou/;

      The first 2 uses in this code will not trigger the recompling of any regex. The 3rd will need to be compiled, although I believe you could do...

      my $re = qr!yes|no!; my $re2 = qr!\sthankyou!; print "yes or no!\n" if "yes" =~ $re; print "yes or no only" if "maybe" !~ $re; print "polite yes or no\n" if "yes thankyou" =~ /$re$re2/;

      ..and avoid any recompilation after the initial qr's

      cheers,

      J

        You'll want to revisit Re^2: meaning of /o in regexes. If you interpolate qr// compiled expressions you lose because the thing is compiled more often than it needs to be. Your "I believe you could do..." is incorrect.

      You cannot apply modifiers to a regex object after it has been defined. Rewrite this as qr!yes|no!o. And the answer to your question is that yes the o modifier comes into play with qr// and locks in the regex the first time it is used.
        Thanks! Now it makes sense.

        But it does make me question one other point. If I don't use the /o in the qr!yes|no!o, if I loop over a set of values using the qr'd regex, does it recompile it every time?

        There is no emoticon for what I'm feeling now.

Re: Re: regex "o" modifier
by pzbagel (Chaplain) on May 06, 2003 at 23:29 UTC

    Well, technically, yes it would come into play since you regex has a variable in it that needs to be interpolated. Adding the o would insure that it is compiled only once. However, since you are only using the regex once, it doesn't matter that much. However, if you were checking that regex 100,000 times, it would make a big difference.

    Cheers