#!/usr/bin/perl use Inline C => <<'EOC'; void foo() { printf("something being sent to stdout\n"); fflush(stdout); } EOC my $output; { # save original STDOUT open my $saved_stdout, ">&STDOUT"; # create a pipe, which we'll use to read from our own STDOUT local(*RH, *WH); pipe RH, WH; # connect the writing side of the pipe to STDOUT, with # STDOUT being (and remaining) fileno 1 (!) open STDOUT, ">&WH" or die "open: $!"; # debug: verify that fileno really is 1 printf STDERR "fileno(STDOUT): %d\n", fileno(STDOUT); # call the C/XS function whose stdout we want to capture foo(); # close WH to avoid buffering issues (pipes are buffered) close WH; # read output (one line) $output = ; close RH; # restore original STDOUT open STDOUT, ">&", $saved_stdout or die "open: $!"; } print "output of foo(): $output"; #### fileno(STDOUT): 1 output of foo(): something being sent to stdout