in reply to Why do we need to close filehandles?

Most of the time you'll be fine if you don't explicitly close your find handles.

However, if you do any further operations on that file before closing, you could end up with unexpected results.

There was actually a case recently where someone was trying to serve a dynamic pdf from apache. They wrote it to a file and then had apache serve the file. Unfortunately, they didn't close the file handle before passing it to apache so their downloaded pdf was always malformed.

The lesson is always close your file handles and/or use lexical file handles so they are closed when they go out of scope.

  • Comment on Re: Why do we nned to close filehandles?

Replies are listed 'Best First'.
Re^2: Why do we need to close filehandles?
by tej (Scribe) on Apr 30, 2011 at 08:35 UTC

    Filehandles are closed automatically at the end of script but if script teminates abruptly giving some error then what happens?..

    downloaded pdf was always malformed : does it affect txt files as well?.. Or just some formats are affected?

      Filehandles are closed automatically at the end of script but if script teminates abruptly giving some error then what happens?..

      They go the the bit bucket.

      downloaded pdf was always malformed : does it affect txt files as well?.. Or just some formats are affected?

      Dear tej, incomplete file is incomplete :) please read Filehandle, File Handles (Windows)