the basic problem is that, in the op's code, the object created by 'new child' simply has no information about any of the child's antecedents, only about the child. therefore, sending an object reference 'up' to person is pointless.

this seems like a good place to use a 'clone' object creation approach rather than a 'new' creation from scratch; each new object cloned from an old object can incorporate the information of the old object and add new information.

package Person; sub create { my $class = shift; my $me = shift; # name of new being # should do some validation here return bless { me => $me } => $class; } use constant ANON => Person->create('unknown'); # some folks have no +antecedents sub bears { my $mom = shift; # mother instance (matrilineal clone) my $me = shift; # kid's name my $dad = shift; # father instance # should do some validation here return bless { me => $me, mom => $mom, dad => $dad } => ref $mom +; } sub ident { my $self = shift; return $self->{me}; } sub mom { my $self = shift; return exists $self->{mom} ? $self->{mom} : ANON; } sub dad { my $self = shift; return exists $self->{dad} ? $self->{dad} : ANON; } my $adam = Person->create('Adam'); # these folks are without antecede +nts my $eve = Person->create('Eve' ); my $able = $eve->bears(Able => $adam); my $mary = $eve->bears(Mary => $adam); my $jane = $eve->bears(Jane => $adam); print "first guy: @{[ $adam->ident ]} \n"; print "first guy's mom: @{[ $adam->mom->ident ]} \n"; print "Jane's mom: @{[ $jane->mom->ident ]} \n"; print "Jane's dad: @{[ $jane->dad->ident ]} \n"; my $fred = $mary->bears(Fred => $able); my $jill = $jane->bears(Jill => $able); print "Fred's dad: @{[ $fred->dad->ident ]} \n"; print "Fred's mom: @{[ $fred->mom->ident ]} \n"; print "Fred's dad's dad: @{[ $fred->dad->dad->ident ]} \n"; print "Fred's dad's mom: @{[ $fred->dad->mom->ident ]} \n"; print "Fred's mom's dad: @{[ $fred->mom->dad->ident ]} \n"; print "Fred's dad's dad's dad: @{[ $fred->dad->dad->dad->ident ]} \n"; print "Fred's dad's dad's dad's dad: @{[ $fred->dad->dad->dad->dad->id +ent ]} \n"; print "Fred's dad's dad's dad's dad's dad: @{[ $fred->dad->dad->dad->d +ad->dad->ident ]} \n"; print "Fred's dad's dad's mom's dad's dad's dad: @{[ $fred->dad->dad-> +mom->dad->dad->dad->ident ]} \n"; my $alan = $jill->bears(Alan => $adam); print "Alan's dad: @{[ $alan->dad->ident ]} \n"; print "Alan's dad's dad: @{[ $alan->dad->dad->ident ]} \n"; print "Alan's mom: @{[ $alan->mom->ident ]} \n"; print "Alan's mom's dad: @{[ $alan->mom->dad->ident ]} \n"; print "Alan's mom's mom: @{[ $alan->mom->mom->ident ]} \n"; print "Alan's mom's mom's mom: @{[ $alan->mom->mom->mom->ident ]} \n";

In reply to Re: my $self is stumped! by Anonymous Monk
in thread my $self is stumped! by headybrew

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.