sub whereami { my $self = shift; $self->{parent}->locate($self); } # in Parent sub locate { my $self = shift; my $child = shift; foreach my $x (0 .. $xmax) { foreach my $y (0 .. $ymax) { return ($x, $y) if $self->{children}[$x][$y] eq $child; } } }

So that is how to do it - use the reference as a string. But do you want to do that?

TMTOWTDI. You could also store x and y information in the child objects. The parent could have a simple array of kids and grep through them for the right X and Y. Or you could pass the x and y on child creation and store the info in two places. Make sure that if you change that info, you change it in both places - use a single subroutine for changing x and y. Again, the subroutine could either be in the Parent class or the Child class, but it will operate on both objects.

A method is just a subroutine with an extra argument.

sub foobar { my $foo = shift; my $bar = shift; # do stuff }

is no different from

sub foobar { my $bar = shift; my $foo = shift; # do stuff }

as a subroutine. So in that sense, where you put your info is not that relevant. (Of course, inheritance is different. But that may not be an issue for you.)

So how do you choose? Well: do objects need to know the x and y of other objects? If so, that info needs to be publicly accessible in the Parent, using the locate method above. If objects only need to know where they are themselves, keep info in the Child.

Also, it looks to me as if x and y refer to co-ordinates. If so, do you really want a 2d array? This will make it easy to find columns:

 @{ $self->{children}[$col] };

but awkward to find rows:

grep {$_->[$row]} @{$self->{children}}; # I think!

There is probably a solution on CPAN for grids... maybe Meta::Geo::Pos2d ? if not, there is certainly a full featured Array class that will make this stuff easier.

dave hj~


In reply to Re: Child objects querying parent objects about themselves by dash2
in thread Child objects querying parent objects about themselves by vaevictus

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.