![]() |
|
Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re-runnably editing a file in placeby eyepopslikeamosquito (Archbishop) |
on May 22, 2003 at 09:06 UTC ( #260011=perlquestion: print w/replies, xml ) | Need Help?? |
eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question: I want to edit a file "in place", preserving the file's permissions, yet be safely re-runnable should an interruption occur at any time. Though the script does not need to be multi-user safe, I am vaguely interested in how that might be achieved (I guess with file locking). I know about Perl's -i switch, but this switch first renames the file, so if the user CTRL-C's (or power is lost) after the rename but before the new file is written, the script is not re-runnable because the original file has been renamed (or may not be complete). Is that right? Anyway, here is my attempt at solving this problem.
This has at least one flaw. In the case of a read-only file, if you are interrupted after chmod($mode|0200) but before chmod($mode), the file $fname will have the wrong permissions on re-run. Improvements welcome. References Added Later
Back to
Seekers of Perl Wisdom
|
|