in reply to Find and Replace from File List

I find the handling of the files curious:

  1. The original input file ($ARGV[0]) is copied to *.bak
  2. The *.bak file is then opened as read only (input) and original file is opened in write mode (to be overwritten)
  3. The *.bak file (input) is deleted after the original ($ARGV[0]) file has been modified.

At the end of the program, you no longer have the original input data. This seems dangerously odd. Is this the correct and intended behavior?

Replies are listed 'Best First'.
Re^2: Find and Replace from File List
by Marshall (Canon) on May 31, 2010 at 17:03 UTC
    Actually the odd thing about this is use of .bak to save the original while working on the original copy instead of using original.new as a scratch pad to work on the new version!

    One consideration is to the extent possible, leave the system in a "known state" even if whole O/S bombs or program bombs, meaning even some function which the modification program called "bombed" and process was killed.

    So in general, I would recommend making a copy of the original file and modify that "new copy" until we are happy with it. Leave the original alone! The last thing to do is to close the ".new" copy, unlink the original, then rename the ".new" version to the original name. The idea is to make the "time window" where the machine is in an unknown state (between "old" and "new") as small as possible.

    The "yeah, butt's" with this are legion. On some O/S's, I can unlink a file, put a new file with same name in directory and folks that have that say old.dll open continue to use the old version because once a file is open, the name is irrelevant...the program is using a file handle, not a name in a directory. New programs that start will get new.dll. A good Windows installation program will register what is called a "run-once" program. This program runs if the OS reboots during the install to clean things up. The installer will remove this "run-once" deal after all it wanted to do was successful (so it doesn't run on the next re-boot).

    Already talked too long about this, but there are some definite "yeah, butt's" to be considered when modifying files. I would move "old-previously-modified" files to some area where they are just history of no real consequence after some period of time. And then periodically delete them.

Re^2: Find and Replace from File List
by aquarium (Curate) on May 31, 2010 at 06:56 UTC
    Hi, What perldoc is that in?..as the perldoc's i've looked at don't say anything about deleting original file. thanks.
    the hardest line to type correctly is: stty erase ^H