A student in my Learning Perl class asked about what shows up in a capture when you apply a quantifier to that group. The great thing about computer programming is that you can just try it to find out:
my $_ = 'abc def ghi jkl';
if( /(\S+) \s+ (\S+\s+)+ (\S+)/x ) {
print "
1: $1
2: $2
3: $3
";
}
else {
print "No match!\n";
}
In that code, $2 comes from that capture sub pattern (\S+\s+), which can match one or more times. Does it get all the things it matched or just the last one? The output shows that it’s the last one:
1: abc 2: ghi 3: jkl
The same thing happens for named captures, even those though are already designed to potentially remember many captures:
use Data::Dumper; $_ = 'abc def ghi jkl'; if( /(\S+) \s+ (?\S+\s+)+ (\S+)/x ) { print Dumper( \%-, \%+ ); } else { print "No match!\n"; }
It only gets the last one too:
$VAR1 = {
'two' => [
'ghi '
]
};
$VAR2 = {
'two' => 'ghi '
};
This isn’t documented in perlre or anywhere else that I could find.