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

perlfaq6:

In versions 5.6 and later, Perl won't recompile the regular expression if the variable hasn't changed, so you probably don't need the /o option. It doesn't hurt, but it doesn't help either.

perlop:

... the largely obsolete /o ...
The bottom line is that using /o is almost never a good idea.

Then what about something like benchmark below (this is 5.42, and I _do_ see a small but noticeable performance bump with real code which does some heavy lifting when parsing. I don't think any variables have changed; just wanted to split pieces of a regex into separate variables for clarity):

use strict; use warnings; use feature 'say'; use Benchmark 'cmpthese'; my $pat = qr/\d+/; my $s = '123abc' x 1e6; cmpthese -1, { foo => sub { 1 while $s =~ /\G $pat \D+ /cgx }, bar => sub { 1 while $s =~ /\G $pat \D+ /cgxo }, }; __END__ Rate foo bar foo 1213522/s -- -88% bar 10345112/s 752% --

Replies are listed 'Best First'.
Re: Are perlfaq6 and perlop correct about "/o" modifier?
by choroba (Cardinal) on May 09, 2026 at 13:14 UTC

      (Oops, shouldn't have used "/c" in benchmark, and should have noticed unrealistic results for such a long input. I'm sorry. Copied "/cg" and "\G" from RL code, thought maybe it's relevant for how "/o" behaves. The premise in the title is still the same.)

      Thanks for the link, that's scary. Perhaps I'd better not "split" a regex into smaller chunks, let it stay long.

Re: Are perlfaq6 and perlop correct about "/o" modifier?
by ikegami (Patriarch) on May 09, 2026 at 17:08 UTC

    First, I removed the incorrect /c which totally breaks the benchmark.

    Then I added a qr-using solution since one might argue that it's qr that "obsoletes /o".

    use strict; use warnings; use feature 'say'; use Benchmark 'cmpthese'; my $pat = qr/\d+/; my $s = '123abc' x 1e6; my $re = qr/\G $pat \D+ /x; cmpthese -1, { "o=0" => sub { 1 while $s =~ /\G $pat \D+ /gx }, "qr" => sub { 1 while $s =~ /$re/g }, "o=1" => sub { 1 while $s =~ /\G $pat \D+ /gxo }, };
    Rate o=0 qr o=1 o=0 3.12/s -- -8% -59% qr 3.39/s 8% -- -55% o=1 7.55/s 142% 123% --

    ...and we still get a large divide. Your point remains, but it now has a valid foundation.

    Note that /o is only 140% faster and not 750% faster. That's probably because of your /c bug.

      Note that /o is only 140% faster and not 750% faster.

      I ran this under a few versions of Perl and /o is 200% faster with 5.42.0 because qr and o=0 are slower!

      5.42.0:

            Rate  o=0   qr  o=1
      o=0 4.17/s   -- -14% -67%
      qr  4.85/s  17%   -- -61%
      o=1 12.5/s 200% 158%   --
      
      5.38.2:
            Rate  o=0   qr  o=1
      o=0 5.22/s   -- -17% -58%
      qr  6.31/s  21%   -- -50%
      o=1 12.5/s 140%  98%   --
      
      5.16.3:
            Rate  o=0   qr  o=1
      o=0 8.26/s   --  -4% -38%
      qr  8.57/s   4%   -- -36%
      o=1 13.4/s  62%  56%   --