Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Auto flock

by ColonelPanic (Friar)
on Nov 20, 2012 at 10:50 UTC ( [id://1004689]=note: print w/replies, xml ) Need Help??


in reply to Re: Auto flock
in thread Auto flock

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:
use subs 'open'; sub open (*;$@) { print "Open sesame!"; #Need logic as Dave outlined for calling CORE::open return CORE::open [...]; } open my $fh, '>file.txt' or die "Ouch!";

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

Replies are listed 'Best First'.
Re^3: Auto flock
by davido (Cardinal) on Nov 20, 2012 at 17:46 UTC

    Your assessment of how to fix some of the concerns I raised is good. One could certainly build logic into the tool to deal with read versus write. And the failed flock issue could also be handled internally with better logic. There are other issues as well though. One is that code isn't usually written with the notion that 'open' could block, which is exactly what would happen with 'autoflock' if a file is already locked. That flock normally blocks to wait for a lock is expected behavior of that function, but not expected of open. That could break existing code.

    So those are the issues we know about. My fear is the edge cases we haven't considered and aren't aware of. If we were talking about overriding 'chop', I'd say no big deal. open is a little more complicated, and the stakes are higher.

    Yes, space_monk's suggestion is probably the one.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1004689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found