http://qs1969.pair.com?node_id=502562


in reply to Accessors in xs

There are several problems with your code. As others have said, you're returning one array-ref instead of two scalars, which accounts for your test failures. But there are also some more subtle problems which could cause core dumps if the caller does something wrong. I would write it as:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" MODULE = My::Games::Azure::Unit PACKAGE = My::Games::Azure::Unit PROTOTYPES: DISABLE void world_location(self) HV *self PREINIT: SV **x, **y; PPCODE: x = hv_fetch( self, "x", 1, 0 ); y = hv_fetch( self, "y", 1, 0 ); EXTEND(SP, 2); PUSHs(x ? *x : &PL_sv_undef); PUSHs(y ? *y : &PL_sv_undef);
The things to notice are:
  1. The self parameter is declared as an HV*, which causes the XS wrapper to issue automatic conversion and type checking code, so you'll get an error if someone accidentally calls it as My::Games::Azure::Unit->world_location() or whatever.
  2. The SV** pointers returned by hv_fetch() are tested to see if they're null (which they will be if the hash entry doesn’t exist). In that case we just return undef. Otherwise you risk a core dump again.