Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Is there another way to get named captures

by Fletch (Bishop)
on Jan 08, 2022 at 21:06 UTC ( [id://11140278]=note: print w/replies, xml ) Need Help??


in reply to Is there another way to get named captures

As it shows in perlvar if you use English there's also %{^CAPTURE} and %{^CAPTURE_ALL} respectively:

## Being lazy and using the Mojo "ojo" oneliner for it's dumper r() $ perl -Mojo -MEnglish -E '$_ = q{foo = bar};m{(?<name>\w+) \s* = \s* +(?<value>.*) $}x; say r(\%{^CAPTURE})' { "name" => "foo", "value" => "bar" }

That being said (IMHO) eliminating every single magic variable isn't really getting you that much of a gain for the extra typing you're setting yourself up to do (${^CAPTURE}{name} vs $+{name}). Some things are just idiomatic perl and going out of your way to make things more cumbersome to type for (again, IMHO) minor "readability" improvements isn't really helpful.

Edit: Or if you read perlvar more carefully it mentions Tie::Hash::NamedCapture which lets you create a named alias which was exactly what you'd asked for. Derp. Since this lets you give the tied hash a semantically meaningful name yourself I'd say that's marginally more valid, but still a bit of . . . not overkill, but maybe more effort than it's worth for a (small) benefit over $+{foo}.

$ perl -Mojo -MTie::Hash::NamedCapture -E '$_ = q{foo = bar};m{(?<name +>\w+) \s* = \s* (?<value>.*) $}x; tie my %h, q{Tie::Hash::NamedCaptur +e}; say r(\%h)' { "name" => "foo", "value" => "bar" }

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

Replies are listed 'Best First'.
Re^2: Is there another way to get named captures
by LanX (Saint) on Jan 08, 2022 at 21:53 UTC
    > %{^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

      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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-03-28 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found