in reply to Re^2: Identifying unmatched data in a database
in thread Identifying unmatched data in a database

Hi AppleFritter,

Granted, Perl is doing its best to do what you mean. And this means, inter alia, that it will flush the write buffers and close the file when the filehandle goes out of scope or when the program completes. So, most of the time, closing explicitly a filehandle seems to be unnecessary. But I still think it is good practice to explicitly close your filehandles (in Perl and in other languages automatically closing filehandles on exit) because:

- It makes your intent clearer to the chap that will have to maintain your code (and we all know that, six months from now, that chap may be you or I);

- The earlier a file is closed, the earlier resources associated to it are freed;

- The earlier a file is closed, the smaller the risk is to use it wrongly;

- the earlier an output filehandle is closed, the earlier the written file is in a stable form. If your program crashes violently, it might not be able to flush the write buffer to the file and close it properly before aborting. If the file was closed cleanly beforehand, everything is fine.

For these reasons, I (almost) always close explicitly my files, especially the output files, as soon as I no longer need them.

Having said that, I must admit that usually don't test the result of the close function.

Edit:

I had not seen Athanasius's answer when I wrote mine. I might not have answered if I had seen it.

  • Comment on Re^3: Identifying unmatched data in a database