Q2: Regarding the reason for being "truly paranoid" -- is this because we don't want another running instance of this script to be writing to $new while we are,
Nope, You're stuck on the $new temp file when the $old original file is what you should be concerned about. You should be using File::Temp to get a uniquely named $new temp file.
I'm not sure I completely understand the hazards of "clobbering." So...
It's when this happens:
There are 2 problems - UserA's changes only last a split second but generally the more important problem is UserB never saw changes UserA made.UserA UserB Orig File Open $orig Original Content | Reads $orig Opens $orig | | Modify $orig in Memory Reads $orig | | Write $orig to FS Modify $orig in Memory UserA Content | Write $orig to FS UserB Content
Q3: Is the problem the fact that $new might exist already because another instance of this script running at the same time had created $new a split-second ago in connection with its own update of $old, and that our process will destroy the contents of that $new due to the way ">" works,
Nope (at least if you use File::Temp). You only have to be concerned about the file has the unchanging name. That is when 'clobbering' occurs.
Q4: In a multi-user environment, does a careful programmer need to use "sysopen/flock LOCK_EX/truncate" every time a script needs to write a file? And now a final wrinkle on the addition of a file lock for $new in the recipe.
Depends.
Q5: Wouldn't we would want to keep $new open (and hence the LOCK_EX in place) until after the "rename( $new, $old )"?
You're still stuck on $new but, I'll rework your question towards what I think you want to ask. 'When should I be releasing a lock'
The best strategy IMO is to create a '.lock' file and flock that. Like this:
|
|---|