How many substitutions took place in a Perl s///g?

musiKk picture musiKk · Jun 23, 2010 · Viewed 8.1k times · Source

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).

Answer

Michael Carman picture Michael Carman · Jun 23, 2010

s/// does return the number of substitutions made in scalar context. From perlop (emphasis added):

s/PATTERN/REPLACEMENT/msixpogce
Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).

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. Since s/// returns a single value (in both list and scalar context) the number of elements is always one even if the s/// didn't do anything.

perl -E "$s='aaabbcc'; $c=()=$s=~s/x/y/g; say qq'$c-$s'"  # prints "1-aaabbcc"

To call s/// in scalar context, omit the =()= pseudo-operator.

perl -E "$s='aaabbcc'; $c=$s=~s/a/x/g; say qq'$c-$s'"  # prints "3-xxxbbcc"