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 | |
by Fletch (Bishop) on Jan 09, 2022 at 04:46 UTC | |
by AlexP (Pilgrim) on Jan 09, 2022 at 11:39 UTC |