in reply to Failed to close file

I can't reproduce that here. What does $! contain? See also the advice in "open" Best Practices.