You need to seek back to the beginning to write to the beginning. You're leaving the stream pointer at the end of the file, so you want to reset it. You may also want to look at truncate because you'll be removing characters, and you'll want to remove excess file.
It is simpler, however, to write to a new file, delete the original, and rename the new file to the old name. Or, rather than delete the original, rename it to a backup name. Much safer, too.
| [reply] [Watch: Dir/Any] |
You can't read and write to the file at the same time like that. You're certainly free to use the same file handle, and that looks like that's what you meant to do with the open modes you chose. Just read in the whole file, then seek to the beginning, write out what you want to, and truncate it to the length of what you've written.
Example:
open(FILE, "+<$file");
my $temp;
while(<FILE>) {
$temp .= $_;
}
seek(FILE,0,0);
print FILE $temp;
truncate(FILE,tell(FILE));
close(FILE);
Of course, you'll want to add some dies to that.
If you're worried about memory usage, I'd suggest reading the file in one line at a time, writing out to a temp file one line at a time, then just moving the temp file over the original (don't forget to preserve permissions). | [reply] [Watch: Dir/Any] [d/l] [select] |
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;
}
| [reply] [Watch: Dir/Any] [d/l] |