Dear monks,

I am trying to apply the OO cookbook example for Inline::C to a situation where one has objects related in a parent->offspring relationship, so that you can instantiate a node (e.g. with a name, a description, and a branch-length), instantiate another node, and set one as the parent of the other - and subsequently retrieve it. Alas, I have to admit I don't know enough about the perl guts to know what to return from a $child->get_parent call. Has anyone else worked along similar lines (but *with* success)? I've appended an example of what I'm trying to achieve. Thank you for any and all pointers!
# Please fill in all missing code and fix the bugs below ;-) #--------------------------------------------------------- #!/usr/bin/perl use strict; use warnings; my $node = Node->new('Homo_sapiens', 'Hominid', 1.234); my $parent = Node->new('Homo_neanderthalensis', 'Hominid', 5.02); $node->set_parent($parent); # $node->get_parent; #--------------------------------------------------------- package Node; use Inline Config => BUILD_NOISY => 1; use Inline C => 'DATA', NAME => 'Node'; __DATA__ __C__ typedef struct treeNode { char* name; char* desc; double branch_length; struct treeNode *parent; } Node; SV* new(char* class, char* name, char* desc, double branch_length) { Node* node = malloc(sizeof(Node)); SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); node->name = savepv(name); node->desc = savepv(desc); node->branch_length = branch_length; sv_setiv(obj, (IV)node); SvREADONLY_on(obj); return obj_ref; } void set_parent(SV* obj, SV* parent) { /* this ought to work, no? */ ((Node*)SvIV(SvRV(obj)))->parent = (Node*)SvIV(SvRV(parent)); } SV* get_parent(SV* obj) { /* ...erm...? */ } void DESTROY(SV* obj) { Node* node = (Node*)SvIV(SvRV(obj)); Safefree(node->name); Safefree(node); }

In reply to Trees using Inline::C by rvosa

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.