in reply to XSub won't write to memory file

I have answered this question a couple of years ago on stackoverflow.com:

The Perl API provides PerlIO_exportFILE() which can convert a PerlIO handle with a file descriptor to a stdio `FILE` pointer. Since PerlIO::Scalar is an "in-memory" file handle without a file descriptor the conversion cannot succeed. The only portable way to pass a `PerlIO::Scalar` handle would be to flush it to a temporary file. The less portable way would be to use a stdio that supports callbacks, like the BSD implementation, funopen(3)

-- chansen

Replies are listed 'Best First'.
Re^2: XSub won't write to memory file
by syphilis (Archbishop) on Jul 10, 2015 at 11:20 UTC
    The Perl API provides PerlIO_exportFILE() which can convert a PerlIO handle with a file descriptor to a stdio `FILE` pointer

    Yes, I mentioned in my first post that I had also taken a look at that option.

    Since PerlIO::Scalar is an "in-memory" file handle without a file descriptor the conversion cannot succeed

    Thank you - that explanation makes sense to me.
    One other thing that I should probably try out is the earlier (anonymous) suggestion of staying within the perlio framework:
    use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; void to_FH(PerlIO * stream) { PerlIO_printf(stream, "hello"); PerlIO_flush(stream); } EOC my ($got1, $got2); $got1 = get_string_1(); chomp($got1); # just in case ... print "OK 1\n" if $got1 eq "hello"; $got2 = get_string_2(); chomp($got2); # just in case ... print "OK 2\n" if $got2 eq "hello"; sub get_string_1 { # Write to a temporary file open TEMPFILE, '+>', undef or die $!; to_FH(*TEMPFILE); seek TEMPFILE, 0, 0; my $ret = <TEMPFILE>; close TEMPFILE; return $ret; } sub get_string_2 { # Write to a memory file my $out; open MEM, '>', \$out or die $!; to_FH(*MEM); close MEM; return $out; } __END__ After compilation, outputs: OK 1 OK 2
    Works fine.
    Thanks guys.

    Cheers,
    Rob