|Just another Perl shrine|
Re: Replacing a string in a fileby graff (Chancellor)
|on Jul 17, 2003 at 04:10 UTC||Need Help??|
The suggestion about file locking above is pertinent, but perhaps not adequate, because there may be some "loopholes" when trying to lock the file that you're actually editing (see the article cited in the code commentary below).
Better to use a "semaphore" file -- one that has no content itself, but serves only to control access to the file that you intend to edit. A semaphore module that I've used to good effect is included below, together with a snippet to show its usage.
Apart from preventing simultaneous edits on a single file, there are other common sources of "corruption" that you need to watch out for:
The first point is just a matter of error checking when writing and closing the file, and/or comparing the input and output file sizes (or the tails of the two files). The second can be solved by running "diff old.file new.file" in a back-tick (qx) operator. The third is rather tricky and application-dependent (good luck with that, if it happens to be an issue for you).
All in all, to be careful, you want to stick with your current basic approach (with file locking and validation added): (a) get a lock on the file, (b) read it and create an edited version of it, (c) validate the edited version, (d) rename the edited version to replace the original, (e) release the lock. As a rule, it's good to keep the duration of the locking to a minimum, but if I understand your task, there's no reducing the five steps mentioned (a-e).
Here's the semaphore-file module that I use in a unix/solaris environment --
And here is how I would normally use it:
(Update -- Aug. 8 2005: had to remove the obsolete url, because the TPJ article is not reachable that way anymore.
(Another update -- Nov. 7, 2006: www.tpj.com is gone. Sincere thanks to davebaker for finding that article again: http://interglacial.com/~sburke/tpj/as_html/tpj23.html)