syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Well ... I think it's a C question ... and therefore ot.

On win32 I run the following Inline::C script:
use warnings; use Inline C => <<'EOC'; void foo() { printf("HELLO"); } EOC for(1..3) { foo(); print "\n"; }
This outputs exactly what I expect and want - namely:
C:\_32\pscrpt\inline>perl try.pl HELLO HELLO HELLO C:\_32\pscrpt\inline>
But on my linux (mandrake-9.1) box the same script produces (transcribed):
[rob@localhost inline]$ perl try.pl HELLOHELLOHELLO[rob@localhost inline]$
I'm wanting to see essentially the same as appears on the win32 box - that is, I want to see the same as would be output by print "HELLO\nHELLO\nHELLO\n";.
How do I get that output on linux by modifying only the foo (Inline::C) function ? Is it possible ?

Cheers,
Rob

Replies are listed 'Best First'.
Re: [OT]: Flushing XS buffers portably
by derby (Abbot) on Aug 31, 2007 at 13:01 UTC

    Check out perlapio ... you shouldn't be using stdio for printing but perl's IO abstraction interface:

    use warnings; use Inline C => <<'EOC'; void foo() { PerlIO_printf(PerlIO_stdout(),"HELLO"); } EOC for(1..3) { foo(); print "\n"; }

    -derby
      Dammit, derby, you found the escape clause :-)

      In real life, foo calls a third party C library function that does use stdio for printing ... something like:
      use Inline C => <<'EOC'; #include <mpfr.h> void foo(mpfr_t * x) { mpfr_out_str(*x, .......); } EOC
      The Inline::C script that I presented was the closest thing I could think of that would actually depict the problem generically ... but, as I can now see, I stuffed up.

      So ... although your response answers the question I asked, it doesn't really address the question I was hoping to be answered ... my fault, not yours.

      I guess I'm really after a way of flushing the stdio printf buffer - sort of an stdio way of doing $| = 1; (if such exists).

      Btw, I had long wondered about the advantages of using the PerlIO_* routines as recommended in perldoc perlclib (and other places). Your reply provides an excellent demonstration of those "advantages". Thank you.

      Cheers,
      Rob
        I guess I'm really after a way of flushing the stdio printf buffer - sort of an stdio way of doing $| = 1; (if such exists).

        Have you tried fflush(3)?

        (It's not autoflushing, though; you have to call it whenever you want to have the buffers flushed...)