in reply to while =~ consecutive matching problem

The problem here is that the word boundary for the first match is used up, and no longer matches for the second match. Something like the following will fix that:
while ($doesntCountAll =~ /\s$pattern\s/g) { $count++; pos($doesntCountAll) -= 1; }
However, the method given by Eimi Metamorphoumai is better.