It is not advisable to mix calls to output functions from the stdio library with low-level calls to write() for the file descriptor associated with the same output stream
So ... there will be no problem if foo() and print() write to different streams. And if foo() and print() do need to write to the same stream then there's *nothing* I can do to ensure that the output will not be garbled. Is that what the above quote is telling me ?
I'm just trying to get an understanding of what *can* and *can't* be done here. I know I can't use fputs() in XSubs (I've tried), but I had hoped it would be possible to have ab XSub safely and sanely call an external C library function that uses fputs().
Thanks, shmem.
Cheers, Rob | [reply] |
It makes no difference whether you call fputs() from XS code or via a wrapper of an external library function - fputs() lives in the stdio implementation and does the same for both XS and library code.
And since stdio has its own ideas about buffering, I guess the safest thing to do is calling fflush() as you did. All that with much handwaving, since I'm not proficient with C.
I remember having read remarks and caveats wrt to stdio, and advice to use the PerlIO layer instead. Sorry for being too
lazy to delve into docs/sources to bring up those bits... maybe another monk has them present? - thanks ;)
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] |
It makes no difference whether you call fputs() from XS code or via a wrapper of an external library function
On win32, at least, there's certainly a difference - in that things get even worse if you call fputs() directly from XS code. The following compiles but there's a runtime error that results in a pop-up telling me that "perl.exe has stopped working". Probably a segfault:
use warnings;
use Inline C => <<'EOC';
void foo() {
fputs("hello world", stdout);
}
EOC
foo();
I remember having read remarks and caveats wrt to stdio, and advice to use the PerlIO layer instead
Yes (eg perldoc perlclib) ... but the C library function still expects a FILE* (not PerlIO*) as an argument, and it's still going to do an fputs(), so I'm pessimistic about the chances of that being helpful.
Cheers, Rob
| [reply] [d/l] [select] |