If the file is relatively small you could just read all of it into memory and then overwrite the file after you have made your changes.
#!/usr/bin/perl
use strict;
use warnings;
my @contents;
my $file = 'done.txt';
open IN, '<', $file or die "Could not open $file for reading.";
while (<IN>) {
$_ =~ s/yahoo/never/g;
push @contents, $_;
}
close IN or die "Could not close $file after reading.";
open OUT, '>', $file or die "Could not open $file for writing.";
foreach my $line ( @contents ) {
print OUT $line or die "Failed to write to $file ($line)."
}
close OUT or die "Could not close $file after writing.";
|