http://www.perlmonks.org?node_id=476094


in reply to How to close all open file descriptors after a fork?

Maybe this is not what you want. But why don't you open all of your FDs as "lexical FHs" (recommended in any case, IMHO) and push them into an @array. You can later close $_ for @ARRAY if needed. (I prefer "lexical FHs" because they will automatically closed on scope exit1 - I'm aware that sockets require an explicit close, though.)

Update: incidentally



1 Or more precisely when no more references exist.

Replies are listed 'Best First'.
Re^2: How to close all open file descriptors after a fork?
by bart (Canon) on Jul 19, 2005 at 14:38 UTC
    I prefer "lexical FHs" because they will automatically closed on scope exit
    No they won't. Your array will still hold a copy and they all will be kept open.

    These file handles are references, are they not? You could try weaken these references in the array, and they'll automatically go away as normal — leaving just an undef in place.

      No they won't. Your array will still hold a copy and they all will be kept open.
      I meant: "in a general situation". In this particular one I suggested to explicitly close the entries kept in the array. I also included a footnote mentioning the ref thing.
      These file handles are references, are they not? You could try weaken these references in the array, and they'll automatically go away as normal — leaving just an undef in place.
      This is a very interesting suggestion, although I have no experience myself weakening references. I won't try because this is not my problem, but I will keep it in mind for when it may be of some use for me...
Re^2: How to close all open file descriptors after a fork?
by nneul (Novice) on Jul 19, 2005 at 13:59 UTC
    The problem is the "unknown context". This is in a subroutine that needs to spawn a background task, called from code that is not under my control. Therefore, I don't know what FDs/FHs are open.
      Well, in that case you may want to choose a way, to "spawn a background task" that will give you the pid of that task, and hopefully if you're under an OS that will let you access the relevant info for that process, you could check that.

      For example under Linux you will be able, and may want to examine "/proc/$pid/fd/".

Re^2: How to close all open file descriptors after a fork?
by nneul (Novice) on Jul 19, 2005 at 14:56 UTC
    I've seen that mentioned before about the & style sub calls, old habits... Got any pointers to more details about that being obsolete?
      Yes, of course I should have mentioned it in my first reply (and I thought I did): perldoc -q perlsub.