open (INFILE, "input.txt"); open (OUTFILE, ">output.txt"); while (INFILE) { $_ =~ s/foo/bar/gms; print OUTFILE "$_\n"; }