Jesse Luehrs wrote:
MooseX::AttributeTree seems to be exactly what you're looking for here.
It also handles a lot of other edge cases that your basic implementation
doesn't, such as inherited attributes.
Thanks for the quick answer. I missed that module indeed, and at first
glance it seems to do what I need, but unfortunately it doesn't. It
expects the parent attribute's value to be an object on which it then
invokes a method with the same name as the child attribute, while I want
just its value. For instance, consider the following example:
package TestMooseTrack;
use Moose;
use MooseX::AttributeTree;
has color => (
is => 'rw',
default => 'red',
);
has line_color => (
traits => [ TreeInherit => { parent_link => 'color' } ],
is => 'rw',
);
package main;
my $t = TestMooseTrack->new;
print $t->line_color;
Now if I run that I want it to print 'red', but instead it yields an error:
$ perl test_attr.pl
Can't locate object method "line_color" via package "red" (perhaps you
forgot to load "red"?) at
/data/wre/prereqs/lib/perl5/site_perl/5.10.1/MooseX/AttributeTree/Accessor.pm
line 50.
Looking at the code of MooseX::AttributeTree::Accessor, it indeed
fetches the value of the parent attribute and then invokes a method on it.
2) If not, is the implementation of the trait correct? What bothers me
most is extending the private _inline_get sub, which seems wrong
(although MooseX::Worm does that too).
But your implementation looks fine, Moose extensions are intended to be
able to override any part of the implementation of things. Privacy
really just indicates which parts of the API are useful for people to
use for introspection (i.e. there's no reason for anyone to actually
call _inline_get themselves, which is why it's private).
Ok, thanks for clearing that up.
Martin