in reply to Named captures or positional variables

Maybe off topic, but I'm meditating about the most elegant way to put a regex into a $var such that its automatically expanded to "(?<var>$var)" when constructing bigger REs.

Maybe with a tied hash %N redefining fetch...

This would not only be DRY but also allow full control about all capture names...

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Named captures or positional variables
by RonW (Parson) on Dec 11, 2014 at 00:34 UTC

    Maybe something like use revar name => "regex";

    or use revar '$name' => "regex";

    (Which are similar to use constant or use vars)

    Note: I suspect this would create "our" variables. Is it possible for a pragma to create "my" variables in the use-ing package?

      > Is it possible for a pragma to create "my" variables in the use-ing package?

      If lexical variables are already declared in the calling package, you can change their value, e.g. with PadWalker.

      Otherwise it wouldn't compile under strict if you try to use them.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Declaration can be worked around using parser hacks. That's what Object::Util does. It declares and populates lexical variables for the caller. (Though it falls back to package variables if it's unable to do the parser hacking stuff.)

Re^2: Named captures or positional variables
by Anonymous Monk on Dec 11, 2014 at 01:14 UTC