in reply to Re^9: Reusing a complex regexp in multiple spots, escaping the regexp
in thread Reusing a complex regexp in multiple spots, escaping the regexp

I think that's what /o was invented for

If a regexp contains something interpolated, I don't know if /o can do much for it. Consider:

my $foo = qr/foo/; while ( get_line($fh) =~ m/($foo)(bar)/ ) { ...; $foo = qr/FOO/; # $foo gets changed }

Even if $foo doesn't change, the fact that it has the potential to change makes any attempt at avoiding recompiling the larger regexp broken.

perldoc perlre does note that /o is pretty broken.

Replies are listed 'Best First'.
Re^11: Reusing a complex regexp in multiple spots, escaping the regexp
by LanX (Saint) on Apr 16, 2026 at 17:01 UTC
    You are showing a semantic where /o MUST NOT be applied. Obviously you shouldn't inhibit recompilation if you need recompilation.

    One of the links sleet listed upthread (or the docs🤔) were explicit that /o would inhibit any recompilation of the regex even with nested variables, hence solving the performance issue in a loop.

    > perldoc perlre does note that /o is pretty broken

    Dunno, a literal quote would be helpful.

    A concrete test - probably with re debug - even more. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Dunno, a literal quote would be helpful.

      Substitution-specific modifiers described in "s/PATTERN/REPLACEMENT/ms +ixpodualngcer" in perlop are: e - evaluate the right-hand side as an expression ee - evaluate the right side as a string then eval the result o - pretend to optimize your code, but actually introduce bugs r - perform non-destructive substitution and return the new value
        I did some research and couldn't find a bug introduced by /o.

        Please demonstrate one, which is not just someone complaining that it acts differently, or misunderstood the o for optimize instead of once.

        It's like complaining about state being buggy since it acts differently to my ...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery