On Thu, Feb 18, 2010 at 05:37:56PM +0100, Martin Kamerbeek wrote: > Hi, > > For a project I need (slave) attributes that default to the value of > another (master) attribute, and keep following their master's value > until they are set with a value of their own. > > At first I tried using lazy attributes with code ref defaults that would > fetch the value of the attribute that is to be tracked. That doesn't > work however, since the value is fetched only on the first get, and > after that stays static even if the master attribute's value changes. > > In the end I ended up writing a trait, based on the MooseX::Worm trait > that came by this list a couple of days ago. > > It can be found here: > > http://gist.github.com/307789 > > Although it seems to do the job, I have a few questions: > > 1) Is there a simpler way of doing this? I've searched the docs, CPAN > and google but I couldn't come up with an answer.
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. > 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). -doy