This is, IMHO, one of the things in Perl that really wreaks of old age.

The issue here is when the FILE symbol in the symbol table is trashed. Since it is global, this happens only when the program exits (global destruction).

Exceptions to this are dynamically and lexically scoped file handles. With the traditional construct (open FILE simply creates FILE at the global scope) this is pretty voodoo.

{ local *FILE; # declare every FILE symbol ($FILE, @FILE, as well as + the handle) local open FILE, "<", "foo"; } print "open" if fileno(FILE); # nope.

What happens in that example is that all the symbols from file are recreated when local is called, and then the new versions exist in place of their predecessors, until they go out of scope. Then *FILE is rolled back, and the new values are destroyed. So in effect, the FILE filehandle is destroyed, and thus closed, and the nothing that was there before local is restored in it's place.

The "new" way to open filehandles, which exists from 5.8, is to pass an uninitialized lexical variable to open. I like this much better because it doesn't work with global symbols at all, and you don't have to use gensym from Symbol to generate "anonymous" filehandles.

{ open my $fh, "<", "foo"; print $fh "blah"; } # $fh is gone here

The IO::Handle family of modules implements further encapsulations - each file handle becomes an object.

To summarize, the filehandle is closed when the variable disappears, and probably the best way to get fine grained control of when variables disappear is to use variables that are not bound to global symbols, which aren't as flexible, but lexical ones. See Dominus's Coping with Scoping for more info on this.

-nuffin
zz zZ Z Z #!perl

In reply to Re: When do filehandles close? by nothingmuch
in thread When do filehandles close? by Sprad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.