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"); } #### while (<$in>) { $code = s/\band\b/$word/ig; # <- Here you make all the substitutions ## UPDATE: ## $code += s/\band\b/$word/ig; # <- Here you make all ## Sorry for the mistake (thanks blazar!) print($out "$_\n"); }