in reply to
In-place editing of files (was: general perl question)
If your program is not being run on the command line you
could use code similar to this.
my $filename = 'foo.txt';
open IN, "< $filename" or die "Can't open $filename: $!\n";
open OUT, "> $filename.bak" or die "Can't write to $filename: $!\n";
while (<IN>) {
s/foo/blah/g;
print OUT $_;
}
close IN; close OUT;
rename "$filename.bak", $filename;
However this code is only the barest code that will work,
and should probably not be used without file locks and other
sanity checks.