docdurdee has asked for the wisdom of the Perl Monks concerning the following question:
From Moose and arrays of objects, adding weak_ref => 1 to the foos attribute in Bar is not the way to go. Is there a stable, reasonable way to do so or should I work up another solution?{ package Foo; use Moose; use namespace::autoclean; has 'bars' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Bar]', default => sub { [] }, handles => { push_bars => 'push', all_bars => 'elements', }, lazy => 1, ); sub BUILD { my $self = shift; $_->push_foos($self) foreach $self->all_bars; } __PACKAGE__->meta->make_immutable; 1; } { package Bar; use Moose; use namespace::autoclean; has 'foos' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Foo]', default => sub { [] }, handles => { push_foos => 'push', all_foos => 'elements', }, lazy => 1, ); __PACKAGE__->meta->make_immutable; 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Arrays of circular memory references
by Athanasius (Archbishop) on Aug 26, 2013 at 04:29 UTC | |
by docdurdee (Scribe) on Aug 26, 2013 at 13:23 UTC | |
by Anonymous Monk on Aug 26, 2013 at 14:16 UTC | |
by docdurdee (Scribe) on Aug 26, 2013 at 14:51 UTC |