The following minimal example defines a wrapper around PerlIO_write
:
MODULE = My::FH PACKAGE = My::FH
INCLUDE: const-xs.inc
int
write_fh (SV* fh, SV* str)
CODE:
STRLEN len
char* buf = SvPV(str, len);
PerlIO* io = IoIFP(sv_2io(fh));
if (io) {
RETVAL = PerlIO_write(io, buf, len);
} else {
croak("cannot use fh as a PerlIO handle");
}
OUTPUT:
RETVAL
Using the write_fh
function on a filehandle that has been created
using open $fh, '<', \$buf
works as expected. However, a tied
filehandle created using the following snippet is not turned into a
PerlIO handle:
my $fh = Symbol::gensym;
tie *$fh, 'My::TIEFH', \$buf;
My::TIEFH
contains the required methods and writing to it via print
$fh $str
works just as expected.
What do I need to do to write to the tied filehandle from XS land?