[guest - Tue Feb 8 21:05:38 2005]:
Show quoted text> > The attached patch fixes all flush() methods to return "0 but
> > true".
>
> Very funny. I think the author of the IO::Handle docs meant that 0
> should be returned, but non-zero is an error. I can't figure out how
> to interpret it any other way.
It is meant literally.
Show quoted text> if ($foo->flush() == 0) {
> print "Everything flushed fine.\n";
> }
Even though it would work (see the very bottom of this message),
"$foo->flush() == 0" is not Perl-ish, it is C-ish. In Perl, this should
just read...
if ($foo->flush()) {
print "Everything flushed fine.\n";
}
...i.e. flush() returns a true value if it succeeds.
Show quoted text> Note, I think this is the described behaviour since fflush(3) returns
> 0 on success. Calling it should be familiar to C programmers.
Perl's flush() _does_ behave similarly to the system call. It returns
numerically 0, but logically true (to make it work Perl-ish).
Show quoted text> It really doesn't seem like much from IO::* is doing this correctly.
> I'm very confused. IO::Handle doesn't even define it, but has it in
> EXPORT_OK. Is it returning undef too then?
Yes, you are confused. Please read the explanation of T_SYSRET in
ext/XS/Typemap/Typemap.xs (in the Perl source):
| The T_SYSRET typemap is used to process return values from system
| calls. It is only meaningful when passing values from C to perl
| (there is no concept of passing a system return value from Perl to
| C).
|
| System calls return -1 on error (setting ERRNO with the reason) and
| (usually) 0 on success. If the return value is -1 this typemap
| returns C<undef>. If the return value is not -1, this typemap
| translates a 0 (perl false) to "0 but true" (which is perl true) or
| returns the value itself, to indicate that the command succeeded.
Believe me, this is how it is supposed to work. For example:
$ perl -e 'print("0 but true" + 0, "\n");'
0
$ perl -e 'print("0 but true" ? "true" : "false", "\n");'
true