in reply to Changing the output filehandle while iterating input files.
See the docs for eof in perlfunc. It gives a recipe on how you can use eof() to detect the end of each file.