in reply to Procedural and object oriented interface in Perl/XS

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

Replies are listed 'Best First'.
Re^2: Procedural and object oriented interface in Perl/XS
by stewa02 (Novice) on Oct 24, 2014 at 05:49 UTC
    I implemented your snippet in my XS and it worked properly. Then I started again to test why my code didn't work. It was simply a problem of declaration: if I declare the SV * zahl inside the if-else statement the compiler somehow doesn't get that the variable will be declared anyways and starts to throw a massive amount of errors at me. Your code did work because you avoided the declaration of this additional SV *. Somehow I feel quite dumb now. Thanks for the help, all!