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
    What would the code look like for your suggestion to write to a temp file and then rename?

    I got the old new replacements to work now by removing the quotes in my two open statements.

    I am still having an issue with my loops. When I run the script it touches every file found two times (not necessary) and I am not sure how I told it to do that.

    Also it is not advancing to the next line in the master file as the print line in the output is not showing the ip address from the second line. Only the first line all the way down.
        :) Thank you.