|Pathologically Eclectic Rubbish Lister|
Re^3: Moose - naliaby stvn (Monsignor)
|on Mar 22, 2008 at 15:19 UTC||Need Help??|
It looks also as if trigger provides the instance to the coderef whilst builder does not?
Actually (as jasonk points out) both 'builder' and 'default' get the instance as their first arg. Of course, if the attributes are not marked as 'lazy', then that instance may not be built/initialized completely. My basic rule is that if you want to use the value of another attribute in your 'default' or 'builder' then make that attribute 'lazy'. The performance costs are minimal (I never actually benchmarked it, but knowing what it does under the covers it can't be too bad), and it will save you any headache later on (since it basically constructs using hash ordering, it might work fine today, and break tomorrow given the random-ness of hash ordering). More information about lazy can be found in Moose::Cookbook::Recipe3.
Moose is not so much lacking in docs as well designed enough to be immediately useful! Excellent work.
Thanks, I blame @Larry (aka - Perl 6 designers) and the various designers of CLOS, Smalltalk, Ruby and OCaml, as I stole all the good ideas from them. But we still need to clean up the docs more. As was discussed on #moose last night, the Moose::Cookbook tend to be the best way to learn Moose, it seems to be most digestable through example rather then straight POD docs on all the meta-objects and such.
Perhaps rather than use trigger, I should be overriding the constructor? I'm using trigger for creating the initial state of the object
Again, as jasonk said, it is better to use 'default' and 'builder' for initializing state, it is what they are intended for. The 'trigger' option is actually run every time the value is set.
As for overriding &new, that is generally not a good idea with Moose as it disqualifies your class from getting an inlined constructor when you make it immutable. It is better to use things like 'default', 'builder' or 'trigger', or if those are not enough you can use the &BUILD method (this is documented in the description for &BUILDALL in the docs for Moose::Object), and if that still is not enough people will use 'around' to override &new (which is something we plan on making optimizable, although it currently is not, future-proofing++).