in reply to Re: Is there another way to get named captures
in thread Is there another way to get named captures

> %{^CAPTURE}

is just %+ by another name° and wouldn't solve the OP's issue with it having global sideeffects, which could lead to unexpected results.

DB<34> 'ABCD' =~ /(?<a>\w)(?<b>\w)(?<c>\w)(?<d>\w)/; $h_matches = \% +{^CAPTURE}; say Dumper $h_matches $VAR1 = { 'b' => 'B', 'c' => 'C', 'a' => 'A', 'd' => 'D' }; DB<35> x $h_matches 0 HASH(0x2fa07f0) empty hash # oh oh ...

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

°) not an alias tho, they have different refs

Replies are listed 'Best First'.
Re^3: Is there another way to get named captures
by Fletch (Bishop) on Jan 09, 2022 at 04:46 UTC

    Aaah I've apparently missed (as in misread) what they're looking for and why (thinking that they're objecting to the punctuation-ness). Once you mentioned that I was thinking maybe the tied version captured %+ at the point of tying but experimenting shows it's affected by the global state so no dice there.

    I could maybe imagine roughing out some sort of "matching state" context object that captures (ha) all of this stuff and returns an independent instance but that's getting back along the lines of your sample in that you'd need to use something else rather than native m{} or what not.

    use Hypothetical::MatchState qw( matchit ); my $match1 = matchit( $_ => $regex ); my $match2 = matchit( $other => $regex ); say $_->named( q{foo} ) for ( $match1, $match2 );

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Great idea for a small module!