in reply to Re^2: isa() on any scalar
in thread isa() on any scalar
I've made a promise that I won't change my 'parent' method, but if I just modify some things, I can make 'node' store an array of children, so my $self->parent( $parent ) call might do something like push @$parent->{ children } $self;. Would that be a bad thing?
Yes it would. Better would be to have the parent itself take responsibility for such caching, by supplying a suitable method:
sub children { my $self = shift; $self->{cached_children} ||= do { # find the children, and end up with (say) an array \@result; }; @{ $self->{cached_children} }; }
By having the method that knows how to find the children be responsible for caching it, someone using a different implementation gets to override the children method, supply the appropriate logic for locating the children, and make their own judgement on whether to cache or not.
In an ideal world, then, any instance method would never modify the innards of any object except the instance it was called on - any other objects should be accessed by method calls only.
Hugo
|
|---|