Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Child objects querying parent objects about themselves

by robartes (Priest)
on Nov 13, 2002 at 18:10 UTC ( #212655=note: print w/replies, xml ) Need Help??

in reply to Child objects querying parent objects about themselves

You could create accessor functions in the parent that access the children arrays, like:
# Parent sub get_child_id { my $self=shift; my $child=shift; # Find the index of the child. Left as exercise for the reader (read +: I'm too lazy :) ). return $child_index; }
Then your child can get its ID using code such as:
# Child sub whereami { my $self=shift; my $parent=$self->{'parent'}; my $id=$parent->get_child_id($self); }
Note that you have a small error in your child code, BTW: you store a reference to the reference to the parent instead of the parent ref itself. Change the line:
my $self = { parent => \$parent; };
my $self= { parent => $parent }; #$parent is a ref, you were storing a + ref to $parent


Update: Changed child sub name to original poster's name: sub whereami.

Replies are listed 'Best First'.
Re: Re: Child objects querying parent objects about themselves
by vaevictus (Pilgrim) on Nov 13, 2002 at 19:09 UTC
    Thanks. As far as the \$parent typo, i've already got it correct in my code... just not when i typed it into SOPW. Thanks for the heads up though.

    As for the method you described ... this is probably the best way to do it... naturally... if this becomes too much overhead, i'll have to make the coords redundant somewhere, as described in jdporter's comment.

      An object oriented principial dilema arises from such redundancy - should the child object be responsible for it's location in the parent (knowing and/or maintaining)?
      Some will argue that since the children heriarchly exist within the parent, then the responsibility should only be the parent's...
      This is perhaps analogeous to the question of wether you should use method objects within sibling methods, or access the object internals directly.
      Think about it.

      In addition, perhaps Tie::RefHash may be of use for maintaing a hash of references to the child objects, within the parent. The parent can simply lookup child by it's object reference, and in the hash will be the coordinate values.

      zz zZ Z Z #!perl
        This is exactly my question and dilema-> I believe that the child *is* hierarchly placed within the parent and that the responsibility should only be the parent's. And thus, while implementation is not a difficult situation as far as any of the 3 suggestions from jdporter's comments, the usage of redundancy in just the parent or partly in the child seem extra.

        I believe I will brute force the results currently, but probably be forced to provide a lookup hash in the parent for efficiency.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://212655]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-05-19 05:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (71 votes). Check out past polls.