Delegation is a yet another amazing feature of Moose.
While basic delegation,
handles => { qw/uri host/ }
is nice in that it saves some typing and provides nice self-documentation
facility, but not essential. The part that looks so enticing to me is
Role-based delegation:
does => 'Rain', handles => 'Rain'
It provides a really elegant aggregation facility. What I found when I tried
this is that only *methods* are delegated - not attributes.
This brought me to the question of, are attributes that fundamentally different
from methods? I've been using attributes each time I wanted to skip
computation phase on things that wouldn't change, replacing the code we've
typed so many times:
sub foo{
my $self = shift;
if ($self->{foo} { return $self->{foo}; }
$self->{foo} = long_and_boring_computation();
return $self->{foo};
}
so much nicer:
has foo => (is => 'rw', builder => 'long_and_boring_computation', lazy => 1);
Now if foo() is part of a role, it'll be applied along with other attributes &
methods. So why does delegation work differently and discriminate against the
poor attributes?
Thanks for your thoughts!
Kate