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

Re: Mutually dependent classes

Thread Previous
From:
Mark J. Reed
Date:
August 4, 2010 19:13
Subject:
Re: Mutually dependent classes
Message ID:
AANLkTinrjXUYnaQpY_L5aQZK3VjR8h9DDJENb4K6As7h@mail.gmail.com
Deferring the load until runtime with "require" seems to work, though
it kinda surprises me that the calls to OtherClass->method don't
trigger a warning when OtherClass isn't known about at compile time.

On Wed, Aug 4, 2010 at 8:21 PM, Mark J. Reed <markjreed@gmail.com> wrote:
> 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?
>



-- 
Mark J. Reed <markjreed@gmail.com>

Thread Previous


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