Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Accessors in xs

by robin (Chaplain)
on Oct 24, 2005 at 19:06 UTC ( #502562=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Accessors in xs
Download Code
Re^2: Accessors in xs
by hardburn (Abbot) on Oct 24, 2005 at 21:25 UTC

    Thanks. I've modified the code as above.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://502562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-09-02 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls