in reply to Redirecting XS stdout

You shouldn't be using C IO lib, you should be using Perl's. (Well, I think so at least. I have no experience in this area.)

Update: Yup, that does the trick:

void c_output() { PerlIO_printf(PerlIO_stdout(), "###c_output"); } void c_output_newline() { PerlIO_printf(PerlIO_stdout(), "###c_output_newline\n"); }
>set PERLIO=stdio >perl a.pl ===============dry run============ printing [perl_output_newline] ***perl_output_newline calling [c_output_newline] ###c_output_newline printing [perl_output] ***perl_output calling [c_output] ###c_output =============test_global========== printing [perl_output_newline] calling [c_output_newline] printing [perl_output] calling [c_output] test_global buffer: (***perl_output_newline\n###c_output_newline\n***perl_output###c_outpu +t) >set PERLIO=perlio >perl a.pl ===============dry run============ printing [perl_output_newline] ***perl_output_newline calling [c_output_newline] ###c_output_newline printing [perl_output] calling [c_output] ***perl_output###c_output =============test_global========== printing [perl_output_newline] calling [c_output_newline] printing [perl_output] calling [c_output] test_global buffer: (***perl_output_newline\n###c_output_newline\n***perl_output###c_outpu +t)

Replies are listed 'Best First'.
Re^2: Redirecting XS stdout
by Anonymous Monk on Feb 21, 2010 at 01:12 UTC
    perlclib has a nice list of Internal replacements for standard C library functions
Re^2: Redirecting XS stdout
by innominate (Beadle) on Feb 21, 2010 at 05:33 UTC

    ++ You know what, that's just the key I needed!

    I'm trying to wrap some C with as little modification as possible (staying current with the upstream). I didn't want to replace any IO calls directly, but after *seeing* your PerlIO call it dawned on me to simply override the stdio calls with PerlIO.

    #define fprintf(fh,...) PerlIO_printf(fh,__VA_ARGS__); #define stdout PerlIO_stdout() #define stderr PerlIO_stderr()
    Thanks!
      Exactly. You'd think something like this already exists. Maybe it does. (Google code search?)
        Maybe it does

        fakesdio.h

        It is part of the base perl source code. See also perlsdio.h.

        I think I've figured out why I didn't stumble on this method before..

        After redefining the macros, I discovered the code I'm wrapping is taking the "return early, return often" concept to the extreme by way of exits.

        Oh well, back to the drawing board!

        Thanks again :)