Rebuilding lazily-built attribute when an underlying attribute changes in Moose

Posted by friedo on Stack Overflow See other posts from Stack Overflow or by friedo
Published on 2010-04-25T10:00:05Z Indexed on 2010/04/25 10:03 UTC
Read the original article Hit count: 407

Filed under:
|
|
|

I've got a Moose class with a lazy_build attribute. The value of that attribute is a function of another (non-lazy) attribute.

Suppose somebody instantiates the class with a value of 42 for the required attribute. Then they request the lazy attribute, which is calculated as a function of 42. Then, they have the nerve to change the first attribute!

The lazy one has already been built, so the builder will not get called again, and the lazy attribute is now out-of-date.

I have a solution now where I maintain a "dirty" flag on the required attribute, and an accessor on the lazy one checks the dirty flag and rebuilds it if needed.

However, this seems like a lot of work. Is there a way to handle this within Moose, e.g. using traits?

© Stack Overflow or respective owner

Related posts about perl

Related posts about moose