<?xml version="1.0" encoding="windows-1252"?>
<node id="213" title="perlfunc:close" created="1999-08-24 18:42:10" updated="2005-08-13 00:44:22">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
close - close file (or pipe or socket) handle

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
close 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt;

&lt;P&gt;
close

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
Closes the file or pipe associated with the file handle, returning 
&lt;FONT SIZE=-1&gt;TRUE&lt;/FONT&gt; only if stdio successfully flushes buffers and
closes the system file descriptor. Closes the currently selected filehandle
if the argument is omitted.

&lt;P&gt;
You don't have to close 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt; if you are immediately going to do another [perlfunc:open|open()] on it, because [perlfunc:open|open()] will close it for you. (See
[perlfunc:open|open()].) However, an explicit [perlfunc:close|close()] on an input file resets the line counter (&lt;CODE&gt;$.&lt;/CODE&gt;), while the implicit close done by [perlfunc:open|open()] does not.

&lt;P&gt;
If the file handle came from a piped open [perlfunc:close|close()] will additionally return 
&lt;FONT SIZE=-1&gt;FALSE&lt;/FONT&gt; if one of the other system calls involved fails
or if the program exits with non-zero status. (If the only problem was that
the program exited non-zero &lt;CODE&gt;$!&lt;/CODE&gt; will be set to &lt;CODE&gt;0&lt;/CODE&gt;.) Also, closing a pipe waits for the process executing on the pipe to
complete, in case you want to look at the output of the pipe afterwards.
Closing a pipe explicitly also puts the exit status value of the command
into &lt;CODE&gt;$?&lt;/CODE&gt;.

&lt;P&gt;
Example:

&lt;P&gt;
&lt;PRE&gt;    open(OUTPUT, '|sort &amp;gt;foo')  # pipe to sort
        or die &amp;quot;Can't start sort: $!&amp;quot;;
    #...                        # print stuff to output
    close OUTPUT                # wait for sort to finish
        or warn $! ? &amp;quot;Error closing sort pipe: $!&amp;quot;
                   : &amp;quot;Exit status $? from sort&amp;quot;;
    open(INPUT, 'foo')          # get sort's results
        or die &amp;quot;Can't open 'foo' for input: $!&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;

&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt; may be an expression whose value can be
used as an indirect filehandle, usually the real filehandle name.

&lt;HR&gt;
</field>
</data>
</node>
