my $fh = new FileHandle; @ARGV = ($file); open $fh, ">test.txt"; while ($line = <>) { $line =~ s/$search/$replace/g; print $fh $line; }