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

A whole bunch of qr// requires so much more noise. It's far less readable. It's also far less efficient (though perhaps not enough to matter). But you have a point about testability.

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

Replies are listed 'Best First'.
Re^4: Reusing a complex regexp in multiple spots, escaping the regexp
by sleet (Pilgrim) on Apr 15, 2026 at 18:01 UTC
      Hmm interesting, and it seems there is no consensus what's happening.
      • Dave_the_m says the qr object is always duplicated because matches depend on scope.
      • Aristotle says, using an additional /o modifier closes the performance gap ... but will alter the semantics
      • Demerphq says he doesn't know what's going on.
      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

        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.
Re^4: Reusing a complex regexp in multiple spots, escaping the regexp
by LanX (Saint) on Apr 15, 2026 at 09:42 UTC
    > A whole bunch of qr// requires so much more noise. It's far less readable

    I can't follow...

    I'm sure I could take Hauke's example and translate it 1to1 using 4 variables $INT, $HEX4, $QUOT, $SOMETHING without more lines and any change in readability.

    I'd even argue it'll become better readable because interpolation is a much more common pattern.

    And $INT is 3 characters shorter than (?&INT)

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