creamygoodness has asked for the wisdom of the Perl Monks concerning the following question:

Greets,

I need to open a filehandle in Perl, store the handle in a hash-based object, then call an object method written in XS which will write something to the filehandle. It seems like pulling a filehandle out a hash and dereferencing it ought to be just as easy as doing the same thing with an arrayref or hashref, but the following code doesn't work (PerlIO_write fails and returns -1)...

/* Extract the object hash and the output filehandle. */ SV* obj_sv = ST(0); HV* obj_hash = (HV*)SvRV(obj_sv); SV* out_fh_ref = *(hv_fetch(obj_hash, "fh", 2, 0)); PerlIO* out_fh = (PerlIO*)SvRV(out_fh_ref); err = PerlIO_write( out_fh, buf_start, buf_len );

I'm all but certain the problem is in casting the dereferenced scalar to a PerlIO*, but I've been unable to figure out an alternative. Snooping the code on some CPAN modules, I've found that a few use GvIOn, but that's not documented in the current perlapi, and I'm not sure what it does.

Any advice?

Update: It appears that this is one solution:

PerlIO* fh = IoOFP( sv_2io(out_fh_ref) );

However, the jury is out on whether it is OK to use IoOFP -- it's not documented as a part of the official perlapi.

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Replies are listed 'Best First'.
Re: Passing a PerlIO to XS
by Anonymous Monk on Sep 06, 2005 at 05:41 UTC
    Whats the error message?