Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Track open file handles

by Eily (Monsignor)
on Apr 06, 2017 at 08:19 UTC ( #1187227=note: print w/replies, xml ) Need Help??


in reply to Re^2: Track open file handles
in thread Track open file handles

Without Hash::Util::FieldHash, no reference to the filehandle is kept anywhere (since the hash would only keep a stringified version), so there's nothing preventing it from being destroyed. But it does make sense to stop tracking a filehandle if it can't be accessed anywhere because the variables holding are all out of scope.

I think returning the filehandle is a good idea, although it can be used for cleanup, it should only be a temporary solution until the real issue with the code is fixed. And if someone trying to fix their code just keeps collecting references to their handles without releasing them, the problem probably is not with your module in the first place. Maybe you can keep a weakened reference to the handle though, that way unless the user keeps a copy of that reference, you have access to the handle, without preventing its deletion.

Replies are listed 'Best First'.
Re^4: Track open file handles
by shmem (Chancellor) on Apr 06, 2017 at 08:52 UTC
    Without Hash::Util::FieldHash, no reference to the filehandle is kept anywhere (since the hash would only keep a stringified version)

    But this stringified key / value pair would not be destroyed if its object goes out of scope. It is just that what Hash::Util::FieldHash does for me - the housekeeping. Why shouldn't I use it?

    I think returning the filehandle is a good idea ... Maybe you can keep a weakened reference to the handle though

    Again, that is what Hash::Util::FieldHash does for me. It keeps a weakened reference and uses the decimal value of the adress part (0xdeadbeef) as key into the fieldhash. The weakened reference has magic attached which provides for deletion of the hash entry when it's freed.

    But! returning a weakened reference from get_fds is a good idea. Thank you.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Why shouldn't I use it?

      You should, I misread your answer actually (I thought you said the hash would prevent the deletion of the handles), I agree with you that this feature is useful.

      But! returning a weakened reference from get_fds is a good idea. Thank you.

      Adding it at the last moment, or storing it as a member as soon as the handle is opened (even if that means having two weak reference to the same handle in nearly the same place) is pretty much the same thing. There's a difference when you return $fd => { fd => $fd, %{$fd{$_}} } rather than id_2obj($_) => $fd{$_} though, in the first case, if the user keeps the hashref, it will always have the values it had when acquired, while in the latter, the "close" and "closetime" value will appear when the handle is closed. I like the first version better, because you have to call get_fds to get up-to-date information.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2022-05-26 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?