I'd like to wrap a C++ class that looks something like this (shortened to show only relevant details):

struct fqz { public: fqz(); fqz(fqz_params *p); ~fqz(); int encode(int in_fd, int out_fd); int decode(int in_fd, int out_fd); }

My intended usage in Perl is something like this:

my $fh = Compress::FQZ::Reader->new($fn); while (<$fh>) { # ... } my $fh = Compress::FQZ::Writer->new($fn); while ($foo) { print {$fh} $bar; }

Under the hood, the constructors will: (1) create a new fqz object, (2) create a new object as subclass of FileHandle (with proper mode depending on Reader or Writer subclass), (3) get a file descriptor based on the filename passed, (4) call the encode() or decode() methods with those file descriptors, and then (5) return the FileHandle object to the caller.

The questions I'm trying to address are (1) how to do the conversions between Perl filehandle and file descriptors (I've looked at http://perldoc.perl.org/perlxstut.html#EXAMPLE-9-Passing-open-files-to-XSes which seems to be the correct concept but I'm having trouble understanding how to implement this), and (2) After finishing the reads/writes to the filehandle in Perl, do I need to call some sort of cleanup code explicity to destroy the underlying C++ objects? Can anyone point me to existing code that is doing something of this sort?


In reply to XS filehandle/descriptor conversions and cleanup by jdv

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.