Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 475
0x1F602 Avatar asked Jan 02 '13 16:01

0x1F602


People also ask

What is a Perl Filehandle?

A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle.

How do I close a file in Perl?

To close a file handle, and therefore disassociate the file handle from the corresponding file, you use the close function. This flushes the file handle's buffers and closes the system's file descriptor. If no FILEHANDLE is specified, then it closes the currently selected filehandle.

How do I open a file in Perl?

Perl open file function You use open() function to open files. The open() function has three arguments: Filehandle that associates with the file. Mode : you can open a file for reading, writing or appending.


1 Answers

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).

like image 117
Sylvain Defresne Avatar answered Oct 06 '22 19:10

Sylvain Defresne