I am familiar enough with XS to be able to do most basic things, but I have a problem and don't know the best approach. Essentially it is quite simple. I have some stream based C widgets that take input on STDIN and spew output on STDOUT. Typically these are compiled into little command line widgets so you might do:

widget1 --flag < input_file | widget2 | widget3 > output_file

As you might expect these are pluggable components of a larger system and I was too lazy to write buffering IO. Now I want to access these widgets via perl. Emulating the pipe linking is of course easily handled in C, but what I want to do is be able to pass two open filehandles to my XS code which will take the place of the input_file and output_file streams.

#!/usr/bin/perl open $in_fh, "<$in" or die $!; open $out_fh, ">$out" or die $!; XS_widget( $in_fh, $out_fh ); # reads stuff from $in_fh and writes s +tuff to $out_fh

I presume/hope that there must be some sort of macro that lets me exract the file descriptor out of an SV. I have done some searching and found stuff like SVt_PVGV Glob (possible a file handle) but my attempts to make anything work have failed. 'file' is not a good search term. It seems like it sould be as easy as extract the file pointer, cast it to FILE and away you go but.....

I have tried the R&D method with XS modules I know that take Perl filehandles like HTML::Parser but have found they do the read in perl and pass buffered chunks to the C. Any pointers would be appreciated.

While it would appear simple to just pass the filenames to the XS and let the open happen in C I do actually want to use Perl streams.

cheers

tachyon


In reply to Linking Open Perl Filehandles to Stream Based C code via XS by tachyon

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.