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

In reply to Passing a PerlIO to XS by creamygoodness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.