Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Ilya Zakharevich
October 6, 2010, 10:27 pm
rate this thread
# There is no need for DESTROY to do anything, because when the
# last reference to an IO object is gone, Perl automatically
# closes its associated files (if any). However, to avoid any
# attempts to autoload DESTROY, we here define it to do nothing.
So how do people destroy the handle without close()ing? I think some
POSIX magic should be done, but which one?
Suppose I make a same-fd duplicate (open "<&=11"). Now I want
to get rid of it - but I must not close() the original handle -
hence not close() the duplicate IO::Handle.
Re: destroying IO::Handle without close()ing the file
Looking at Perl_io_close in doio.c suggests using
binmode $FH, ":pop" while PerlIO::get_layers $FH;
before the handle goes out of scope. After this fileno $FH returns -1,
and ktrace confirms that closing it doesn't attempt to call close(2).
I believe, though I haven't tried it, that you could also do this in a
DESTROY method, since they are called before io_close for IO objects.
I don't think there's any other way to destroy the Perl-level object
without closing the OS fd; I don't think normal C stdio provides one
either, which is probably why Perl doesn't.
- » FAQ 2.3 I don't have a C compiler. How can I build my own Perl interpreter?
- — Previous thread in » PERL Discussions