What happens if you don't close a filehandle in Perl?
Asked Answered
A

1

9

If I am writing a Perl script that overwrites STDERR with a duplicate of STDOUT but I never restore the filehandle, what happens at the end of script execution? I can't find anything that warns against what actually occurs or doesn't occur.

I'm probably misinformed but thanks for your patience.

Alabaster answered 2/1, 2013 at 16:44 Comment(0)
O
12

When a process exits, the kernel recovers all used resources. This includes all the file descriptors, which are simply closed. If there is an application-level buffer, data in that buffer may not have been written to the kernel, but otherwise there is no risk in keeping file descriptors open before exiting.

If your Perl script ends by using exec to launch another process, that process will inherit all the file descriptors (except those marked as close on exec).

Oshea answered 2/1, 2013 at 16:51 Comment(1)
Also, perl will normally take care of flushing its own buffers as needed, unless you do something silly like kill 9, $$. Although it appears that there are some minor remaining issues with that on a few platforms.Wilhelm

© 2022 - 2024 — McMap. All rights reserved.