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"); }