On Thu, Jan 29, 2009 at 10:59 AM, Sean Allen
<s...@monkeysnatchbanana.com> wrote:
> I'm playing around with Moose in a real world work scenario and came across
> an interesting question...
>
> I'm working on a series of reports that mostly similar, you have to
>
> build up limits to a general query.
> pull that query
> organize the data
> pull all the data together for display and some math
>
> so you need to get things like:
>
> limits
> value X
> value Y
>
> value X and Y depend on limits to be pulled...
>
> limits is like this:
>
> has 'limits'  => ( is => 'rw', isa => 'Str', builder => 'build_limits' );
>
> could X and Y also be done with builders when they depend on limits being
> set?
> is there a way to order the initialization of attributes? do i have to order
> it? is
> moose smart enough to order them correctly ( pie in the sky there )?

What you want I think is 'lazy'. Basically lazy will make attribute
initialization happen when the attribute is first *called* rather than
when the class is created. Most of the time this is "smart enough" to
make things happen properly so if you make X, Y and limits "lazy" then
when you call X or Y for the first time they'll call limits during
their initialization and cause it to get initialized first. This is
such a common idiom that there is an option for has() called
'lazy_build' which when set to true (lazy_build => 1) does the
equivalent of:

has limits => ( ... builder => '_build_limits', lazy => 1, clearer =>
'_clear_limits', predicate => 'has_limits');

all that wrapped up in:

has limits => ( is => 'rw', isa => 'Str', lazy_build => 1);

-Chris

Reply via email to