I don't think you need those pointers. There's always more than one way to do it :) You can implement all of the functions in XS using the CODE section, or you can implement just few basic ones, and provide the rest of the code in your .pm file. Whatever you find most suitable.
Couple of things you may find useful. The PACKAGE can be used to place the function into different name space.
For example
MODULE=MySession PACKAGE=MySession::submodule
void
my_func()
With this you'll have MySession::submodule::my_func. Also, you don't need to put this declaration before each function. Only where you want to switch the current package/module.
You can bless your scalar into object and then call other functions as methods of the object. Like this
SV *
new(pkg)
SV * pkg
INIT:
if(!SvPOKp(pkg) || SvCUR(pkg) == 0)
croak("Expected package name as argument");
CODE:
{
struct my_struct * wrap;
SV * obj;
HV * stash;
Newx(wrap, 1, struct my_struct);
# init the wrapper
# create perl variable that holds pointer to my structure
# I may need it for the cases when I have to call a perl
# function and provide object to it.
wrap->holder = newSVuv(PTR2IV(wrap));
# get the namespace for blessing
stash = gv_stashsv(pkg, 0);
if(stash == NULL) croak("No stash for package %s",
SvPV_nolen(pkg));
# this is the reference that I shall return
obj = newRV_noinc(wrap->holder);
# Now I bless the object, really the holder of my
# structure is blessed, not the reference to it!
RETVAL = sv_bless(obj, stash);
}
OUTPUT:
RETVAL
But you have to make sure that you declare method DESTROY in your package. This method shall be called
when the last reference to the holder (not to the returned reference!) is gone. At this point you shall
release the memory you have allocated. Of course, you may request that the user calls special function that
releases the object, but this is error prone and not really needed.
void
DESTROY(obj)
SV * obj
INIT:
struct my_struct * wrap;
SV * holder;
IV val;
void * wrap;
if(!SvROK(obj)) croak("Expected reference to object");
holder = SvRV(obj);
if(SvTYPE(holder) != SVt_PVMG)
croak("The object type is wrong");
val = SvIV(holder);
wrap = INT2PTR(struct my_struct *, val);
CODE:
Safefree(wrap);
|