open (INFILE, "input.txt") or die "cannot open input: $!"; open (OUTFILE, ">output.txt") or die "cannot open output: $!"; while () { # note the < and > brackets s/foo/bar/sg; # no =~ needed for $_ # no m needed print OUTFILE; # no "$_\n" needed: $_ is automatic # and the \n is still in $_ (you # don't cho(m)p it... } close INFILE; close OUTFILE;