in reply to Editing Contents of a File
The Perl Cookbook p.243-244 recipe 7.10 shows "Modifying a File in Place with -i Switch" - you basically open the file with "+<", read the whole file into an array, update the array, then rewrite the file and truncate it to its current seek pointer:
If you don't mind using a temporary file, there's another recipe in the same section.open(FH, "+< FILE") or die "Opening: $!"; @ARRAY = <FH>; # change ARRAY here seek(FH,0,0) or die "Seeking: $!"; print FH @ARRAY or die "Printing: $!"; truncate(FH,tell(FH)) or die "Truncating: $!"; close(FH) or die "Closing: $!";
HTH.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Editing Contents of a File
by bivouac (Beadle) on Jun 21, 2004 at 17:09 UTC |