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

Hmm interesting, and it seems there is no consensus what's happening. I think the first and second statement are contradictory or probably hinting at a bug somewhere.

Anyway I'm confused, there may be a difference if a $var= qr/.../ is used directly as a RHS of a match or is interpolated inside a bigger regex.

And since it's possible to stringify a qr-object and use the string instead, this adds another layer of confusion.

I have to think hard about a concise way to test all of this which wouldn't result in a tl;dr monstrum.

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

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

Replies are listed 'Best First'.
Re^6: Reusing a complex regexp in multiple spots, escaping the regexp
by sleet (Pilgrim) on Apr 15, 2026 at 22:32 UTC
    Note the (?(DEFINE)...) construct has it's own performance issue. From (DEFINE):
    patterns defined this way probably will not be as efficient, as the optimizer is not very clever about handling them.
    Also, all DEFINE'd patterns are capturing, even if you don't need it, so that is another minor hit against performance. This can also be confusing if you are trying to refer to capture groups by absolute number instead of by name.
      I'm still not convinced that nesting qr// snippets into larger qr// parts is such a show stopper.

      Are they stored as nested objects causing overhead? (Hmm probably... I read something in the docs about them acting like closures.)

      But what if I stringify/flatten the outcome and recompile it, even with a /o once modifier? This should reset most effects.

      Unless context switches like different nested modifiers like /i cause new overhead...(?)

      DB<1> p qr/[0-9a-f]/i (?^ui:[0-9a-f]) DB<2> $x=qr/[0-9a-f]/i DB<3> $hex_range= qr/$x+ - $x+/x DB<4> say $hex_range (?^ux:(?^ui:[0-9a-f])+ - (?^ui:[0-9a-f])+) DB<5>

      I'll put this on my to-do list, and will try to dive deeper next weekend.

      Like running benchmarks and looking into the op-tree.

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

        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.