You can also read and write to the same file using the IO::InSitu module. An example form the doco seems to suit your requirements (the in and out filenames can be the same)
use IO::InSitu;
my ($in, $out) = open_rw($infile_name, $outfile_name);
for my $line (<$in>) {
$line =~ s/foo/bar/g;
print {$out} $line;
}