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

I'm looking for a way to pass in an open file GLOB to an XS routine. I tried googling on this and have reread the perlxs and perlguts pages several times, but I'm still unclear on the concept.

I know that GLOBs are stored as a "stash" which has a special GV "hash" value, but I don't know how I'm supposed to turn that information into a "real" Unix-ish file descriptor.

Basically, I want to put a snippet like this in my Module.pm:

sub open_with_fd { my $self = shift; my $fh = shift; my $big_buffer = do_funky_xs_thang($fh); return $big_buffer; }
And I want do_funky_xs_thang to be an XSUB that can take an open filehandle and do stuff to it. (The return value will be a big buffer as a scalar value.)

Any help would be appreciated. Even a code snippet somewhere would be a good starting point.

Thanks.

Replies are listed 'Best First'.
Re: How to pass filehandles from perl to XS?
by tlm (Prior) on Jun 28, 2005 at 03:59 UTC

    Try putting this in your XS interface:

    char * do_funky_xs_thang( file ) FILE * file
    Or you can get a file descriptor for any open filehandle using fileno, and pass that (as a simple integer) to your C (or whatever) function, if that's what it needs.

    the lowliest monk

Re: How to pass filehandles from perl to XS?
by fmerges (Chaplain) on Jun 28, 2005 at 09:43 UTC