close FILEHANDLE
This function closes the file, socket, or pipe associated with the filehandle.
You don't have to close FILEHANDLE
if you are immediately going to do
another open on it, since the next open will close it for
you. (See open.) However, an explicit close on an input file
resets the line counter ($.), while the implicit close done by
open does not.
Also, closing a pipe will wait for the process executing on the pipe
to complete (in case you want to look at the output of the pipe
afterward), and it prevents the script from exiting before the pipeline is
finished.[1]
Closing a pipe explicitly also puts the status value of the command
executing on the pipe into
$?.
For example:
[1] Note, however, that a dup'ed pipe is treated as an ordinary filehandle, and close will not wait for the child on that filehandle. You have to wait for the child by closing the filehandle on which it was originally opened.
open OUTPUT, '|sort >foo'; # pipe to sort ... # print stuff to output close OUTPUT; # wait for sort to finish die "sort failed" if $?; # check for sordid sort open INPUT, 'foo'; # get sort's results
FILEHANDLE
may be an expression whose value gives the real filehandle name.
It may also be a reference to a filehandle object returned by some of
the newer object-oriented I/O packages.