|No such thing as a small change|
Re^2: Auto flockby ColonelPanic (Friar)
|on Nov 20, 2012 at 10:50 UTC||Need Help??|
Thanks for this very useful post. I would just like to add that you don't have to override CORE::GLOBAL::open, etc. Instead, you can override open and close in only the current package:
This will solve one of the problems you mentioned, as now you are not overriding modules' use of open and close.
The problem of lock failing wouldn't really be a problem as long as you handled it properly in your open function--just catch any errors there and close the filehandle if lock fails.
The problem of seeking to the end of the file would be pretty easy to handle as well: detect that the file was opened for appending, and only seek to the end if that is true.
Still, I do tend to think that this solution is more hacky than it's worth. Overriding system functions is not really recommended. It makes for confusing code, and there still may be unintended side effects.
I think space_monk's suggestion of making your own, differently named, open and close functions that lock and using them where needed would be easier overall.
When's the last time you used duct tape on a duct? --Larry Wall