MODULE=MySession PACKAGE=MySession::submodule void my_func() #### 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 #### 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);