Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Is there another way to get named captures

by LanX (Sage)
on Jan 08, 2022 at 20:40 UTC ( #11140277=note: print w/replies, xml ) Need Help??

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 %+


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


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 (Monk) on Jan 09, 2022 at 11:34 UTC

    Thank you! I've forgotten this:

    func( { %+ } );

    It's great solution.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140277]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2022-05-26 12:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.