I expected @- and @+ to be the same size after a successful match, but they aren't. For example this script:
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.20.0;
use Data::Dumper;
$Data::Dumper::Terse = 1;
my $rex = '([a-z]+) | ([0-9]+) | ([A-Z]+)';
my $str = '9999';
if ( $str =~ m/$rex/x ) {
say Dumper(\@{^CAPTURE});
say Dumper(\@-);
say Dumper(\@+);
}
produces this output:
[
undef,
'9999'
]
[
0,
undef,
0
]
[
4,
undef,
4,
undef
]
It looks like @- doesn't include undef for trailing unmatched groups, while @+ does. Why the difference?
On the way to an alternative, does this pass for all $str and $rex:
if ( $str =~ m/$rex/x ) {
scalar(@{[$&, @{^CAPTURE}]}) == scalar(@-) or die;
}