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


In reply to Re^3: More Questions about Extending Perl by andal
in thread More Questions about Extending Perl by unlinker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.