Dear inliners,

I am trying to apply the OO cookbook example 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!

Rutger

# 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', 6.02E+23);
my $parent = Node->new('Homo_neanderthalensis', 'Hominid', 6.02E+23);

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



Reply via email to