In addition to what others said:

Filehandles are a limited resource. Typically, a process is limited to 20 to about 200 file handles, trying to open more handles will result in errors. The reason for this limit is that file handles have a price. The OS needs to allocate some memory for the file meta-data (where on the disk is the file, at what offset will data be read/written, and so on) and some more memory for the file contents. Unwritten data has to be buffered in memory, because disk writes and even flash writes are waaaaay slower than writing to RAM. So every common OS attempts to write only when really needed, preferably without blocking the entire system.

Aborting a program which still has file handles opened is unpredictable, because the libc can also buffer data, in addition to the operating system. Wheather or not the data buffered in the libc ends in the file depends on how the libc is implemented and how the program is aborted. Therefore, the general advice is to keep files opened as short as possible, so that libc and OS can flush their buffers to the disk.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Why do we need to close filehandles? by afoken
in thread Why do we need to close filehandles? by tej

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.