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++).

-stvn

In reply to Re^3: Moose - nalia by stvn
in thread Moose - nalia by doc_faustroll

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.