Hi,
I thought I understood how to deal with the mixing of XS and perl buffers ... but apparently not. Consider this simple Inline::C demo script:
## try.pl ##
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'END_C';
void foo(int i) {
printf("From C: %d\n", i);
/* Flush all IO buffers */
fflush(NULL);
}
END_C
for ($i =0; $i < 10; ++$i) {
print "From perl: $i\n";
foo(++$i);
}
There's no problem with that - it outputs (as I expect):
From perl: 0
From C: 1
From perl: 2
From C: 3
From perl: 4
From C: 5
From perl: 6
From C: 7
From perl: 8
From C: 9
But when (with perl-5.10.0, both Linux and Windows) I run the script as
perl try.pl >out.txt I find that out.txt contains:
From C: 1
From C: 3
From C: 5
From C: 7
From C: 9
From perl: 0
From perl: 2
From perl: 4
From perl: 6
From perl: 8
I don't understand why the output has changed - I expected the flushing of the buffers to take care of things for me ... but it obviously didn't. Can anyone enlighten me ?
This is pretty much an academic question - I know of a number of ways to modify that Inline::C script so that the output, when re-directed to out.txt, is as expected/desired. (The simplest way is to replace "printf" with "PerlIO_stdoutf".) But my question is "Why does the above script fail to re-direct correctly ?"
Update: Hmmmm ... when I said "I know of a number of ways ...", the "number" I was thinking of was, in fact, "1" :-)
There are other ways of getting the correct output into out.txt, but when it comes to command line re-direction, the *only* way I know of is to replace "printf" with "PerlIO_stdoutf".
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.