You are viewing the version of this documentation from Perl 5.24.1. View the latest version
%-

Similar to %+, this variable allows access to the named capture groups in the last successful match in the currently active dynamic scope. 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.

Here's an example:

if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) {
    foreach my $bufname (sort keys %-) {
        my $ary = $-{$bufname};
        foreach my $idx (0..$#$ary) {
            print "\$-{$bufname}[$idx] : ",
                  (defined($ary->[$idx])
                      ? "'$ary->[$idx]'"
                      : "undef"),
                  "\n";
        }
    }
}

would print out:

$-{A}[0] : '1'
$-{A}[1] : '3'
$-{B}[0] : '2'
$-{B}[1] : '4'

The keys of the %- hash correspond to all buffer names found in the regular expression.

The behaviour of %- is implemented via the Tie::Hash::NamedCapture module.

Note: %- and %+ are tied views into a common internal hash associated with the last successful regular expression. Therefore mixing iterative access to them via each may have unpredictable results. Likewise, if the last successful match changes, then the results may be surprising.

This variable was added in Perl v5.10.0.

This variable is read-only and dynamically-scoped.