while (<$in>) { s/\band\b/$word/ig; # <- Here you make all the substitutions while (/\band\b/ig) # <-... and here nothing happens! { ++$count; } print($out "$_\n"); }