in reply to Don't close filehandles (was: To Kill a Meme: while(defined($line = <>)) )
in thread To Kill a Meme: while(defined($line = <>))

Isn't it at least theoretically possible for close to return an error? I'm not sure what, if anything could be done to rectify it if it did, but it could be used to alter the course of the rest of the program, even if it was only to log an error and exit.

Is there any way of trapping and handling such an error if you allow an unclosed lexical filehandle to just go out of scope?

I guess if you where using IO::*, you could subclass the DESTROY method, but would the filehandle still be open at that point? Or would an error code be accessible?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!

  • Comment on Re: Don't close filehandles (was: To Kill a Meme: while(defined($line = <>)) )

Replies are listed 'Best First'.
Re: Don't close filehandles (was: To Kill a Meme: while(defined($line = <>)) )
by Abigail-II (Bishop) on Nov 03, 2003 at 21:38 UTC
    Isn't it at least theoretically possible for close to return an error?
    Yes, but that's normally only important for files open for writing, sockets and pipes. But usually you don't really care if you can't close a filehandle that you have open for reading.

    Abigail

      Granted. With respect to the OP's case of reading a file, but I took Aristotle to mean that he advises using locally scoped lexical file handles for the general case.

      I can't honestly claim that I usually check the return from close even on writable files. Little I'm doing is that critical at the moment, but I was wondering how you could handle the possibility of the error occuring with autoclosed filehandles?

      As I said, I'm not sure it would be possible to do much about it beyond logging the fact that a failure had occured, but that could be important for tracking down failures in critical applications. I just can't see a way to go about it when auto-closing lexical handles.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!
      Wanted!

        I almost always check the return value of close on handles that I have open for writing. There's usually not much you can do to recover, but I like my programs to die screaming instead of silently going down. Perhaps the most common cause of a close failure is when the filesystem is full. If that happens, I want to know it right away.

        I certainly wouldn't autoclose filehandles that I have open for writing, because I want to inspect the return value of close. And save from writing some XS code, I don't think you detect a close failure if your handle autocloses. Perhaps setting $! to 0 just before leaving the scope, and inspecting $! right afterwards might work, but other variables can go out of scope, causing $! to be set.

        Abigail