in reply to Re^7: 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. I believe when the larger regexp is being compiled, any nested regexps are simply stringified and interpolated into it.

In the original post that could be a problem as the regexp would need to be compiled each time around the while loop. As long as you're compiling it outside the loop, I don't see it being much of an issue.

  • Comment on Re^8: Reusing a complex regexp in multiple spots, escaping the regexp
  • Download Code

Replies are listed 'Best First'.
Re^9: Reusing a complex regexp in multiple spots, escaping the regexp
by LanX (Saint) on Apr 16, 2026 at 14:39 UTC
    > 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

      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