http://qs1969.pair.com?node_id=11140277


in reply to Is there another way to get named captures

> I wonder if there is another way to get named captured groups than %+ or %-.

not without reimplementing it

> and now it looks:

my %matches = %+; sub(\%matches);

why not just

func( { %+ } );

this will pass the hashref of a flat copy, without messing with the globally tied %+

update

demo in debugger

DB<18> use Data::Dumper DB<19> sub func { say Dumper \@_} DB<20> 'ABC' =~ /(?<a>\w)(?<b>\w)(?<c>\w)/; func({%+}) $VAR1 = [ { 'a' => 'A', 'b' => 'B', 'c' => 'C' } ];

NB: if you are only passing the named captures, why not just flattening %+ to a list?

DB<22> sub func { my %matches = @_; say Dumper \%matches} DB<23> 'ABC' =~ /(?<a>\w)(?<b>\w)(?<c>\w)/; func(%+) $VAR1 = { 'a' => 'A', 'b' => 'B', 'c' => 'C' };

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

edit

s/sub/func/ , since sub is a keyword

Replies are listed 'Best First'.
Re^2: Is there another way to get named captures
by AlexP (Pilgrim) on Jan 09, 2022 at 11:34 UTC

    Thank you! I've forgotten this:

    func( { %+ } );

    It's great solution.