vr has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Data::Dump 'dd'; my $s = "a aa aaa aaaa"; $s =~ /(?<a>a+) (?<a>a+) (?:(?<a>a+)bbb)?/; dd \@{^CAPTURE}; # all captured groups dd $+{a}; # leftmost defined "a" dd ${^CAPTURE}{a}; # ditto dd $-{a}; # all defined "a"'s groups dd ${^CAPTURE_ALL}{a}; # ditto __END__ ["a", "aa"] # correct "a" # correct undef ["a", "aa", undef] "a"
Documentation is not very verbose about new %{^CAPTURE}, %{^CAPTURE_ALL} variables, they are listed as if they are English synonyms to old %+ and %-, but they are obviously not, they look plain wrong to me.
The "aaa" was deleted from @{^CAPTURE} array (or was not even added to begin with), when rightmost cluster failed to match, and deleted as array element from @{$-{a}}, but the $#{$-{a}} was not changed from wrong 2 to expected 1, hence unexpected undefined element in @{$-{a}}.
Update: Actually, w/r/t %-, re-reading the docs, there's no phrase "all defined "a"'s groups" as I stated above.
To each capture group name found in the regular expression, it associates a reference to an array containing the list of values captured by all buffers with that name (should there be several of them), in the order where they appear.
Yes, they say "all buffers with that name", but can undef be said to be "captured"? Can failed sub-expression "capture"? It's ambiguous.
Update 2: Mixing these "CAPTURE" things is broken:
#dd \@{^CAPTURE}; dd \%{^CAPTURE};
is OK, but un-commenting 1st line results in empty unblessed hash in the 2nd.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: %{^CAPTURE}, %{^CAPTURE_ALL} and %- don't produce expected output (updated x3!)
by haukex (Archbishop) on Jun 11, 2019 at 18:15 UTC | |
Re: %{^CAPTURE}, %{^CAPTURE_ALL} and %- don't produce expected output
by haukex (Archbishop) on Jun 19, 2019 at 20:48 UTC |