develooper Front page | perl.perl5.porters | Postings from August 2010

Mutually dependent classes

Thread Next
From:
Mark J. Reed
Date:
August 4, 2010 17:49
Subject:
Mutually dependent classes
Message ID:
AANLkTim1XOELsXewx9my3buhj8kzGG_P4156unEeBhrt@mail.gmail.com
Dumb question maybe, but if I have two classes which need to call
methods on each other's objects, how can I define them without getting
'redefined subroutine' warnings (and possibly assorted other
circular-reference problems)?

Here's the basic model:

package Parent;
use base ModelObject;
use Child;
sub children {
    my $self = shift;
    unless ($self->{_children})  {
         foreach my $child_id (@{$self->{children}}) {
             push @{$self-{_children}}, Child->lookup($child_id);
         }
    }
    return $self->{_children};
}
1;

package Child;
use base ModelObject;
use Parent;
sub parent {
    my $self = shift;
    return $self->{_parent} ||= Parent->lookup($self->{_parent_id});
}
1;

Any suggestions?

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About