package CirculatorySystem;
use Moose;
has 'pressure' => (is => 'ro', isa => 'HashRef[Int]');
package Eyes;
use Moose;
has 'body' => (
is => 'rw',
isa => 'Body',
weak_ref => 1, # cycles are bad
handles => [qw[ blood_pressure ]]
);
package Body;
use Moose;
has 'circulatory_system' => (
is => 'ro',
isa => 'CirculatorySystem',
handles => {
blood_pressure => 'pressure'
}
);
has 'eyes' => (
is => 'ro',
isa => 'Eyes',
trigger => sub {
my $self = shift;
# must hook up new eyes
# to the body ...
$self->eyes->body($self);
}
);
package main;
my $nexus_6 = Body->new(
eyes => Eyes->new, # i make your eyes!
circulatory_system => CirculatorySystem->new(
pressure => { systolic => 112, diastolic => 64 }
),
);
print $nexus_6->blood_pressure;
# is the same as ...
print $nexus_6->circulatory_system->pressure;
# is the same as ...
print $nexus_6->eyes->blood_pressure;
####
has [qw[ eyes ears nose mouth ]] => (is => 'rw');
around [qw[ eyes ears nose mouth ]] => sub { ... code to make accessors return $self here ... }
####
my @parts = qw[ eyes ears nose mouth ];
has $_ => (
is => 'rw',
predicate => "has_$_"
) foreach @parts;