my $file = $ARGV[0]; if (open(IN, "<$file")){ my @text = (); my $modified = 0; local $/ = undef; map { $modified = ($_ =~ s|oldstring|newstring|gs); push(@text, $_) } ; close(IN); if ($modified && open(OUT, ">$file")){ print OUT @text; close(OUT); } }