in reply to Re: FileHandle: undef vs close
in thread FileHandle: undef vs close

Forgive my ignorance, but do you know if using undef leaves the closure of the file handle to garbage collection rather than executing the system call straight away and so is undef more efficient? Just wondering.

Replies are listed 'Best First'.
Re^3: FileHandle: undef vs close
by chromatic (Archbishop) on Apr 30, 2009 at 17:59 UTC

    You're doing IO and a system call and this is a micro-optimization, so this is a lousy place to optimize, but if you want to close the filehandle unambiguously, use close directly. undef removes one reference count from the filehandle; other variables may point to the same filehandle and prevent Perl's GC from reclaiming it (and closing it implicitly).

    Of course, if other variables point to the same filehandle and you close it explicitly underneath them, you may cause other problems.