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

On a tangent, what is the benefit of (?(DEFINE) ...) constructs for repeated patterns here?

Intuitively, I would have opted for interpolating nested $variables holding qr// snippets, especially since I can make them more readable with /x and can unit test them individually.

But I'm curious to learn why you chose this way.

Is it about the handling of capture groups?

I tried to read the relevant docs, but they constantly mention recursion and I can't spot any here...

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

Replies are listed 'Best First'.
Re^3: Reusing a complex regexp in multiple spots, escaping the regexp
by ikegami (Patriarch) on Apr 14, 2026 at 19:18 UTC

    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.

        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

      > 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