Question

I am trying to write XS glue code for a serialization/deserialization library that is able to work with anything that provides a write(ctx, buffer, count)/read(ctx, buffer, count) interface. I'd like to be able to use pseudo-filehandles that I get with

open $reader, '<', \$in;
open $writer, '>', \$out;

so using a FILE* mapping does not seem to work. Since I did not find any good documentation, I played around and arrived at the following XS snippet:

void
write_buf (fh, string);
INPUT:
PerlIO* fh;
SV* string;
CODE:
STRLEN length = SvLEN (string);
char* buf = SvPV (string, length);
PerlIO_write (fh, buf, length);

It seems to do what I want, but is this the proper way of handling everything that Perl may consider a filehandle in XS code?

Was it helpful?

Solution

You have what you need. The functions that work with PerlIO* will indeed handle everything that appears as a file handle to a Perl program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top