Here's an Inline::C demo that does pretty much the same thing, but bypasses the creation of a couple of extra SV's:
use strict; use warnings; use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1; use Inline C => <<'EOC'; double wrap_cos(SV * in) { if(sv_isobject(in)) { HV *self_hv = MUTABLE_HV(SvRV(in)); SV **callback_ptr = hv_fetchs(self_hv, "Number", 0); return cos(SvNV(*callback_ptr)); } return cos(SvNV(in)); } EOC my $angle = 0.5; my $x = {'Number' => $angle}; bless $x; print wrap_cos($x), "\n"; # Output: 0.877582561890373 print wrap_cos($angle), "\n"; # Output: 0.877582561890373
Inline::C is just XS under the hood, and the XS file that it autogenerates and compiles (and can be found under ./_Inline/build) is:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "INLINE.h" double wrap_cos(SV * in) { if(sv_isobject(in)) { HV *self_hv = MUTABLE_HV(SvRV(in)); SV **callback_ptr = hv_fetchs(self_hv, "Number", 0); return cos(SvNV(*callback_ptr)); } return cos(SvNV(in)); } MODULE = try_pl_96f2 PACKAGE = main PROTOTYPES: DISABLE double wrap_cos (in) SV * in
Maybe there's something there that helps you ... or maybe not ;-)
(The header file INLINE.h is generally not necessary, and is not needed here. You'll find it in the same folder as the XS file.)

Cheers,
Rob

In reply to Re: Procedural and object oriented interface in Perl/XS by syphilis
in thread Procedural and object oriented interface in Perl/XS by stewa02

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.