destroying IO::Handle without close()ing the file

  • Thread starter Ilya Zakharevich
  • Start date
I

Ilya Zakharevich

The comment in IO/Handle.pm says:

# 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?

Puzzled,
Ilya

P.S. WHY?

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.
 
I

Ilya Zakharevich

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).

Thanks for a wonderful piece of voodoo!
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top