Closed p5pRT closed 16 years ago
The perl docs for close don't mention that the return status is affected by previous errors on the filehandle.
For example\, in doio.c Perl_do_print returns the status using:
return !PerlIO_error(fp);
Perl_do_close calls Perl_io_close which does:
bool prev_err = PerlIO_error(IoIFP(io)); retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
So close only returns success if the close worked _and_ there was no previous error on the stream.
(Doesn't apply to pipes though\, but if the write to a pipe failed then it's hard to imagine circumstances where the pclose wouldn't return an error.)
This behaviour of close is a good thing. It means that the return status of close can be used to check for complete success: not just success in closing the handle\, but also that nothing went wrong while using the handle.
The perl docs for close don't mention that the return status is affected by previous errors on the filehandle.
[snip]
So close only returns success if the close worked _and_ there was no previous error on the stream.
(Doesn't apply to pipes though\, but if the write to a pipe failed then it's hard to imagine circumstances where the pclose wouldn't return an error.)
The return value of close() is affected by the state of the handle and the state reported by the IO layers. The simple layer
$ cat PerlIO/via/error.pm package PerlIO::via::error; use strict;
sub PUSHED { bless {}\, __PACKAGE__ } sub ERROR { 1 }
1;
always reports an error and thus close() will always "fail" when it is in effect:
$ perl -MPerlIO::via::error -e 'open $f\, "\<:via(error)"\, \ > "/dev/null" or die $!; close $f or warn $!' Warning: something's wrong at -e line 1.
Here is an attempt to mention this behaviour in the docs:
$ diff -urd blead/lib/perl5/5.9.5/pod/perlfunc.pod /var/tmp/perlfunc.pod --- blead/lib/perl5/5.9.5/pod/perlfunc.pod 2006-11-15 17:45:23.000000000 +0100 +++ /var/tmp/perlfunc.pod 2006-11-16 10:33:33.000000000 +0100 @@ -870\,10 +870\,10 @@
=item close
-Closes the file or pipe associated with the file handle\, returning -true only if IO buffers are successfully flushed and closes the system -file descriptor. Closes the currently selected filehandle if the -argument is omitted. +Closes the file or pipe associated with the file handle\, flushes the +IO buffers\, and closes the system file descriptor. The return value +takes into account the state of the handle and its IO layers (if any). +Closes the currently selected filehandle if the argument is omitted.
You don't have to close FILEHANDLE if you are immediately going to do
another C\
The RT System itself - Status changed from 'new' to 'open'
On 16/11/06\, Dintelmann\, Peter \Peter\.Dintelmann@​dresdner\-bank\.com wrote:
Here is an attempt to mention this behaviour in the docs​:
I reworked it a bit\, and applied it :
==== //depot/perl/pod/perlfunc.pod#548 (text) ====
@@ -870\,10 +870\,11 @@
=item close
-Closes the file or pipe associated with the file handle\, returning -true only if IO buffers are successfully flushed and closes the system -file descriptor. Closes the currently selected filehandle if the -argument is omitted. +Closes the file or pipe associated with the file handle\, flushes the IO +buffers\, and closes the system file descriptor. Returns true if those +operations have succeeded and if no error was reported by any PerlIO +layer. Closes the currently selected filehandle if the argument is +omitted.
You don't have to close FILEHANDLE if you are immediately going to do
another C\
p5p@spam.wizbit.be - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#39902 (status was 'resolved')
Searchable as RT39902$