$var is the name of the C variable for a particular funcion argument. $arg is the Perl SV that matches $var. So an "input" typemap sets $var to be some value based on $arg. An "output" typemap sets $arg based on $var. No, I don't recall where that is documented [ other than the source code :( so you might want to read bits of perl/lib/ExtUtils/*.pm from time to time ].

I would not recommend doing C-struct-to-Perl-hash conversion (or vice versa) via a typemap for two reasons:

  1. You'd have to do the conversion once or twice per call and the performance penalty can easily be too high.
  2. This takes at least a few lines of C code and putting that much C code into a typemap is a pain and hard to maintain.

I find it is better to keep C structs either "packed" into a Perl string if possible, or hidden in a Perl integer if necessary (as far as the XS interface is concerned). The first method is possible if the C code that you are interfacing to allows the caller to allocate the memory for the structs. The second method (or something similar or perhaps lots of memory copying) is required if the C API insists on passing back pointers to memory that it allocated itself.

Then you can write pack/unpack and/or insert/extract routines (in XS and/or Perl) and probably provide one or more Perl wrappers that do the pack-then-call-XS-then-unpack to make things easy for certain cases.

There are a ton of ways to write these pack/unpack and/or insert/extract routines. I'd probably have:

$packed= mystructPack( \%unpacked ); $refToUnpacked= mystructUnpack( $packed );
If you want more details then post more details like the specific struct and what you typically do with it.

Also, buffers.h in Win32API::Registry includes some macros for dealing with typemaps of pointers to structs.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: XS module typemap by tye
in thread XS module typemap by cb921

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.