in reply to Re^2: The return value of m// (clues)
in thread The return value of m//

When you use the g modifier, it keeps track of its last match. In the first RE (scalar context), 456 is matched: your count indicates a single match (see code below for actual value). In the second RE (list context), the remaining four sets of digits are matched. You can use pos to reset the last match position.

$ perl -Mstrict -Mwarnings -e ' > my $a=" 456 789 123 456 789 "; > my $count; > $count = $a =~ /\d+/g ; > print "$count\n"; > print "${^MATCH}\n"; > pos($a) = 0; > my @a = $a =~ /\d+/g ; > $count = @a; > print "@a\n"; > print "$count\n"; > ' 1 456 456 789 123 456 789 5

${^MATCH} is described in perlvar.

-- Ken

Replies are listed 'Best First'.
Re^4: The return value of m// (clues)
by sophate (Beadle) on Apr 23, 2012 at 09:41 UTC

    Thanks so much !! It's great to learn more about how g behaves :-)