in reply to Need assistance updating text files with Perl
open (FR, '<', '$filename') or print "Could not read $filename +\n" && return; my @lines = <FR>; close (FR); open (FW, '>', '$filename') or print "Could not open for write + $filename\n" && return; for ( @lines ) { s/$ipold/$ipnew/g; print FW $_; } close FW;
Can I also suggest writing to a temporary file, and then renaming? If you overwrite the old file, you risk a power outage / system failure / SIGINT leaving you with an empty or corrupt file.
--MidLifeXis
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need assistance updating text files with Perl
by Anonymous Monk on Dec 31, 2009 at 18:25 UTC | |
by Anonymous Monk on Dec 31, 2009 at 18:43 UTC | |
by MikeDexter (Sexton) on Dec 31, 2009 at 19:29 UTC |