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

> Pretty sure they're not kept as nested objects.

That was my first intuition, and looks like we were right, the final program is the same for the flattened string.

$ perl -Mre=debug -E'$x=qr/[X0-9a-f]/i; $hr=qr/$x+ - $x+/x;' Compiling REx "[X0-9a-f]" Final program: 1: ANYOF[0-9A-FXa-fx] (11) 11: END (0) stclass ANYOF[0-9A-FXa-fx] minlen 1 Compiling REx "(?^ui:[X0-9a-f])+ - (?^ui:[X0-9a-f])+" Final program: 1: PLUS (12) 2: ANYOF[0-9A-FXa-fx] (0) 12: EXACT <-> (14) 14: PLUS (25) 15: ANYOF[0-9A-FXa-fx] (0) 25: END (0) floating "-" at 1..9223372036854775807 (checking floating) stclass ANY +OF[0-9A-FXa-fx] plus minlen 3 Freeing REx: "(?^ui:[X0-9a-f])+ - (?^ui:[X0-9a-f])+" Freeing REx: "[X0-9a-f]" $

$ perl -Mre=debug -E'$hr=qr/(?^ui:[X0-9a-f])+ - (?^ui:[X0-9a-f])+/x;' + Compiling REx "(?^ui:[X0-9a-f])+ - (?^ui:[X0-9a-f])+" + Final program: 1: PLUS (12) 2: ANYOF[0-9A-FXa-fx] (0) 12: EXACT <-> (14) 14: PLUS (25) 15: ANYOF[0-9A-FXa-fx] (0) 25: END (0) floating "-" at 1..9223372036854775807 (checking floating) stclass ANY +OF[0-9A-FXa-fx] plus minlen 3 Freeing REx: "(?^ui:[X0-9a-f])+ - (?^ui:[X0-9a-f])+" $

> the regexp would need to be compiled each time around the while loop

I think that's what /o was invented for

Update

So... Consequently any performance penalty coming with qr// could be mitigated by flattening it first and using the resulting string with /o modifier. 🤔

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

Replies are listed 'Best First'.
Re^10: Reusing a complex regexp in multiple spots, escaping the regexp
by tobyink (Canon) on Apr 16, 2026 at 15:05 UTC

    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.

      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