No, the former just is not right. You haven't actually tried it, have you?In scalar context we get the count of the matches, in array context we get the matches.$num_matches_stuff = $str =~ m/(stuff)/g; @all_the_matches = $str =~ m/(stuff)/g;
Result:$str = "stuff stuff stuff"; $num_matches_stuff = $str =~ m/(stuff)/g; print $num_matches_stuff;
1
The /g modifier in scalar context is very special. It is intended to be used in a loop, something like this:
Result:$str = "stuff stuff stuff"; while($str =~ m/(stuff)/g) { print "Got one!\n"; }
Got one! Got one! Got one!So in scalar context, it will match at most once at a time — next time around, it'll continue where it left off last time.
Therefore, the returned valued of //g when used in scalar context is either 0, or 1.
In reply to Re: Perl Idioms Explained - @ary = $str =~ m/(stuff)/g
by bart
in thread Perl Idioms Explained - @ary = $str =~ m/(stuff)/g
by tachyon
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |