Here is some typemap code I copied from my own project. I mostly figured this out (aka copy/pasted) from the perlxstut manpage I think.
TYPEMAP # You need a typedef for BarPointOrNull in your .xs file or a header.. +. # Like this: typedef bar_t * BarPointerOrNull; # Then you can use it in the XS parameter list: # routine( a, b, c ) # char * a # int b # BarPointerOrNull c BarPointerOrNull T_BAR_OR_NULL INPUT #--------------------------------------------------------------------- +-------- # Map a perl reference to a C pointer... #--------------------------------------------------------------------- +-------- T_BAR_OR_NULL if ( ! SvOK( $arg )) { $var = NULL; } /* Make sure we are given an object of class Bar::Class... */ else if ( sv_derived_from( $arg, \"Bar::Class\" )) { IV tmp = SvIV( (SV *) SvRV( $arg )); $var = INT2PTR( $type, tmp ); } /* Holy crap! I must have copied this from the manpage... nice. */ else { Perl_croak(aTHX_ \"%s: %s is not of type %s\", ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, \"$var\", \"Bar::Class\") } OUTPUT #--------------------------------------------------------------------- +-------- # Map a C pointer to a perl reference... #--------------------------------------------------------------------- +-------- T_BAR_OR_NULL if ( $var == NULL ) { $arg = &PL_sv_unef; } else { /* We bless the pointer as a Bar::Class object... can be handy * for wrapping structures in objects. */ sv_setref_pv( $arg, \"Bar::Class\", (void *)$var ); } # $var is the C variable, $type is the C type, $arg is the perl variab +le # Um that's all I know. OH and always escape your double-quotes!
EDIT On second thought... this won't work. The XS function will complain about not enough arguments if you don't give it a third argument. You could always wrap it in a pure-perl function, though...
# Silly really... but so easy! sub myroutine { my ($a, $b, $c) = @_; routine( $a, $b, $c ); }

In reply to Re^3: XS: How to enable passing ref or undef? by juster
in thread XS: How to enable passing ref or undef? by tlhackque

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.