Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Filehandles question

by Anonymous Monk
on Dec 10, 2004 at 10:11 UTC ( [id://413794]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I am working on a couple of scripts created by a colleague. They contain a number of filehandles (10 or so) to textfiles. He opens all these filehandles as usual, but nowhere in the script does he close these filehandles. I guess they get closed automatically when the script finishes. Is this just a case of bad coding practice, or are there any other effects on the performance of the script?

Thanks in advance,
Jonathan.

Replies are listed 'Best First'.
Re: Filehandles question
by gaal (Parson) on Dec 10, 2004 at 10:44 UTC
    Depending on how exactly this is coded, this may or may not be a problem.

    1. You don't have to worry about leaking filehandles. The operating system will free resources used by the process. This is true for any OS I can think of that perl runs on.

    2. You may be bitten by bufferring, because your output is not flushed to the file before the script ends.

    3. On non-regular files, the close system call can fail. If you don't close the file yourself, you get no indication of this.

    It's generally considered good practice to close the files, even if none of the above gives you particular reason to worry. (It sounds like #2 might, though.) Some reading: "Suffering from Buffering?", Catching errors in closing lexical filehandles.

      On non-regular files, the close system call can fail. If you don't close the file yourself, you get no indication of this.
      It can fail on regular files, too. If the file system fills up between when the system last flushed your results and now, your close will try to write all the data that's in the buffer to the file and fail.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

Re: Filehandles question
by Crian (Curate) on Dec 10, 2004 at 10:45 UTC
    Perl closes all open file handle when it terminates normaly. I read somewhere that it should be better to let Perl this job instead of closing the files, but I don't remember exactly why. I hope someone else will point this out here later.

      That's a safety feature, but it's better to close files when you're done with them:

      • It makes code maintenance cleaner, since it indicates that you are declaring that you are done with the file
      • Some operating systems (like Windows) have implicit file locking, so closing the file will release the lock and allow other processes to access the file.
      • On some operating systems, a limited number of files can be opened at a time. If you have a lot of processes running that leave files open that they don't need, nobody can open a file.
      • If there's an error or interrupt signal that kills your program, Perl may exit and leave the file open. If there's a lock, you've got a problem. In Windoze, you gotta reboot to clear the lock. Not something you want to do on a server.
      • Similarly, if the program is interrupted without an explicit close, output buffers may not be flushed and you lose data

      Open file handles shouldn't be treated like other variables in terms of garbage collection. The memory may be returned to the operating system when it exists, but Perl has no way of knowing for sure if it should be unlocked or the buffer flushed.... (there may be times where doing so can do more harm than good), so it won't do these things.

      If you don't want to be bothered with file opening and closing semantics, then use something like IO::All.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413794]
Approved by pelagic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found