Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Accessors in xs

by robin (Chaplain)
on Oct 24, 2005 at 19:06 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://502562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-24 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found