On Jul 10, 2004, at 11:18 AM, Tim Pushor wrote:
I would like to make my XSUBS callable in an OO style for various reasons (mostly because of namespace). Do I just make my C XSUBS take a hash reference as the first argument? Then can I just do the same as I would in perl - i.e. store 'class' stuff in the hash?
(sorry if this gets into too much of basics, i'm trying to be complete:)
it's just like perl -- methods are just normal subs. xsub methods are just normal xsubs.
your object must be a blessed reference (doesn't need to be a hash, recall), mostly so that perl's method lookup will work. just use that as your first argument.
MODULE = ModuleName PACKAGE = PackageName
# assuming you have created a typemap for PackageName int methodname (PackageName * this, int arg1, float arg2)
can then be called as
$thing->methodname (@args);
if you want a class static method, e.g., something that gets invoked as
PackageName->methodname(@args);
(e.g. for new()) you'd, just as in perl, ignore the first argument. an easy way to do that while avoiding "unused parameter" warnings from the c compiler is
int methodname (class, int arg1, float arg2)
that is, just don't give a type to the ignored class parameter, and xsubpp won't generate any code for it.
however, unless your methods really are class static methods (e.g. constructors and helpers), don't make them "OO style" just to get the -> syntax. if it's conceptually just a function, it should stay a function. the namespace problem is easily solved by not exporting your functions (which you really shouldn't do anyway without good reason):
Packagename::foo(); # just a function (not overridable)
$thing = Packagename->new (); # class static method (overridable)
$thing->bar; # class method (overridable)
-- Jolt is my co-pilot. -- Slogan on a giant paper airplane hung in Lobby 7 at MIT. http://hacks.mit.edu/