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

I think this (letting perl close file automatically) is a bad idea. It does happen on NFS that close returns an error (when net connection breaks or quota is exceeded). (It can also happen if the disk is full or physically bad.) But Perl only gives a severe warning in this case, not an error, so if you don't see the warning (because cgi script or scrolled out of screen by make's messages), it will seem that it's run normally.
  • 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 05, 2003 at 12:40 UTC
    Again, getting the return value of a close for a handle that's open for writing is useful. But for something that's open for reading?

    I am going to disagree with anyone who states that filehandles should be closed automatically, or who states that automatically closing is a bad idea because of the return value of close, if they don't distinguish the case of having something open for writing, and having something open for reading.

    Abigail

      You are right, I didn't think of that. It is OK to do it when the file is open for read-only.

Re^2: Don't close filehandles
by Aristotle (Chancellor) on Nov 06, 2003 at 20:30 UTC

    It's not something I thought of because nearly all of my scripts only read other files and write results to STDOUT.

    But it doesn't invalidate the approach, as even if I have to check the return value of close, I'll just put the check at the bottom of the scope. A pity that there's no more declarative way to do the check, but anyway.

    If anything, it can still be argued to be better than not using a scope in two ways: a) it reinforces a difference compared to read-only accesses (which won't have a close anywhere) and b) you cannot forget to close the filehandle even if you forget to check whether it was closed properly.

    Makeshifts last the longest.