Small example:
perl -e '$s="aaabbcc";$c=()=$s=~/a/g;print"$c\n$s\n"' (m//g) outputs
3
aaabbcc
whereas perl -e '$s="aaabbcc";$c=()=$s=~s/a/x/g;print"$c\n$s\n"' (s///g) outputs
1
xxxbbcc
I’d like to do both things at once without having to match first: substitute and know the number of substitutions. Obviously a s///g does not return the number of substitutions in scalar context–unlike m//g does with matches. Is this possible? If yes, how?
perlre, perlvar and perlop provided no help (or I just couldn’t find it).
s///does return the number of substitutions made in scalar context. From perlop (emphasis added):Your problem is that you didn’t call
s///in scalar context. You called it in list context and then evaluated the assignment (to an empty list) in scalar context. A list assignment in scalar context returns the number of elements produced by the right-hand side of the expression. Sinces///returns a single value (in both list and scalar context) the number of elements is always one even if thes///didn’t do anything.To call
s///in scalar context, omit the=()=pseudo-operator.