Dear monks,

I'm trying to adapt the OO example of the Inline::C cookbook. I'd like to have setters that return $self. Some stuff sort of seems to be working, but returning $self isn't:
use strict; use warnings; my $parent = Node->new('Parent'); my $child = Node->new('Child'); # this DWIM... (prints 'Parent') print $child->set_parent($parent)->get_parent->get_name, "\n"; # ...but $child is now undef, (Can't call method ... ) $child->set_name('New name'); print $child->get_name, "\n"; ################################# package Node; use Inline C => q{ typedef struct { char* name; double branch_length; struct Node* parent; struct Node* previous_sister; struct Node* next_sister; struct Node* first_daughter; struct Node* last_daughter; } Node; SV* new(char* class, char* name) { Node* node = malloc(sizeof(Node)); SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); node->name = strdup(name); sv_setiv(obj, (IV)node); SvREADONLY_on(obj); return obj_ref; } SV* set_name(SV* self, char* name) { ((Node*)SvIV(SvRV(self)))->name = strdup(name); return self; } char* get_name(SV* self) { return ((Node*)SvIV(SvRV(self)))->name; } SV* set_parent(SV* self, SV* parent) { ((Node*)SvIV(SvRV(self)))->parent = parent; return self; } SV* get_parent(SV* self) { return ((Node*)SvIV(SvRV(self)))->parent; } void DESTROY(SV* self) { Node* node = (Node*)SvIV(SvRV(self)); free(node->name); free(node); } };
...And I get:
% perl inline.pl Parent Can't call method "get_name" on an undefined value at inline.pl line 1 +0.
So what is the correct way to return self so it doesn't turn into undef?

(P.s.: I am user rvosa, but I'm away from my computer, forgot my password and lost the email address the reminder was sent to.)

In reply to OO Inline::C - returning $self not working by Anonymous Monk

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.