2

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?

Rakesh
  • 756
  • 8
  • 10
hillu
  • 9,423
  • 4
  • 26
  • 30

1 Answers1

2

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Thank you. Judging from the sort of things I am learning right now, I seem to have reached parts of Perl that would benefit from some documentation updates... – hillu Sep 20 '12 at 20:46