Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: More Questions about Extending Perl

by unlinker (Monk)
on Jan 02, 2011 at 16:37 UTC ( #880084=note: print w/replies, xml ) Need Help??

in reply to Re: More Questions about Extending Perl
in thread More Questions about Extending Perl

Thanks! That was very clearly explained. I would go with copying that string into XS space and free it myself. Any pointers on the Moose bit?
  • Comment on Re^2: More Questions about Extending Perl

Replies are listed 'Best First'.
Re^3: More Questions about Extending Perl
by andal (Hermit) on Jan 03, 2011 at 09:13 UTC

    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);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://880084]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-05-24 09:07 GMT
Find Nodes?
    Voting Booth?