in reply to Regex /o modifier: what bugs?

To my knowledge, o does what it says it does. I believe the author was hyperbolically stating that using o results in surprises, hard to test code, etc. This doesn't really belong in the doc, if so.

Replies are listed 'Best First'.
Re^2: Regex /o modifier: what bugs?
by Marshall (Canon) on Dec 16, 2022 at 05:43 UTC
    For a regex intensive project many moons ago, I was running the same regex in loop against a big array of entries. I thought that this was the ideal place for the /o option, but I found that it made no performance difference at all. I remember reading somewhere that in some situations, Perl is smart enough to do /o automatically without being asked to do so. That appeared to be the case in my test. Its been so long ago, that I don't remember the exact regex being tested - could be that there wasn't a $var in it to begin with - don't remember.

      Patterns that don't interpolate are always compiled at compile time. /o is useless for these.

      $ perl -Mre=debug -c -e'/a/' Compiling REx "a" Final program: 1: EXACT <a> (3) 3: END (0) anchored "a" at 0..0 (checking anchored isall) minlen 1 -e syntax OK Freeing REx: "a"

      Each instance of a regex operator (m//, s///, qr//) that interpolates caches the last pattern it compiled in string and compiled form. If that instance is evaluated again, and if the generated pattern is the same as the previous one, recompilation is skipped.

      $ perl -Mre=debug -e'$x = "a"; for my $y (qw( b b a a )) { /$x/; /$y/; + }' 2>&1 | grep -i comp Compiling REx "a" Compiling REx "b" Compiling REx "a" Skipping recompilation of unchanged REx "a" /$x/ same as last Compiling REx "b" Skipping recompilation of unchanged REx "b" /$y/ same as last Compiling REx "a" Skipping recompilation of unchanged REx "a" /$x/ same as last Compiling REx "a" Compiling REx "a" Skipping recompilation of unchanged REx "a" /$x/ same as last Compiling REx "a" Skipping recompilation of unchanged REx "a" /$y/ same as last

      It still needs to perform the interpolation, and it needs to perform a string comparison, so it won't be as exactly as fast as using /o. But it should be close.

      > I remember reading somewhere that in some situations, Perl is smart enough to do /o automatically without being asked to do so.

      I remember reading that this depends on the Perl version. And I think I read it here.

      At some point - long ago - Perl became capable to notice that the input pattern didn't change and reused the last compilation, IIRC.

      We should have extensive discussions on this in the archives.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        > We should have extensive discussions on this in the archives.

        I still keep a Regex Benchmark on my scratchpad, but I can't find the discussion that made me create it.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: Regex /o modifier: what bugs?
by kcott (Archbishop) on Dec 15, 2022 at 20:49 UTC

    G'day ikegami,

    ++ Thanks for the very quick response. That's good to know.

    — Ken