in reply to Re: context conversions: seeking enlightenment
in thread context conversions: seeking enlightenment

Well, I can see two potential sources of confusion.

First, the less likely one. Some think that parens (()) impose a list context. So you might think that $s=~/,/g is in a list context here:

my $s=',,,,'; $nCommas=($s=~/,/g);
but it is not. Those parens just make the grouping explicit. [Parens are often used when building lists. And using parens on the left side of an assignment does provide a list context to the right side.]

Second, m/.../g should (usually) only be used in a scalar context as part of a loop. Each time m/.../g is used in a scalar context, only the single next match is found (see pos for some more information).

So in the second block, Perl has only found the first comma and doesn't yet know that there are 4 commas.

Alternatives to your first method include:

my $s=',,,,'; print $s =~ tr/,//; my $count= ()= $s=~/,/g; print $count; print 0+( ()= $s=~/,/g );
See also perldoc perlre and Regexp operators (in perldoc perlop).

        - tye (but my friends call me "Tye")