Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Moose - nalia

by doc_faustroll (Scribe)
on Mar 21, 2008 at 22:23 UTC ( #675542=note: print w/ replies, xml ) Need Help??


in reply to Re: Moose - nalia
in thread Moose - nalia

Thanks stvn,

It looks also as if trigger provides the instance to the coderef whilst builder does not?

Moose is not so much lacking in docs as well designed enough to be immediately useful! Excellent work.

Perhaps rather than use trigger, I should be overriding the constructor? I'm using trigger for creating the initial state of the object.


Comment on Re^2: Moose - nalia
Re^3: Moose - nalia
by jasonk (Parson) on Mar 22, 2008 at 02:11 UTC

    If you just want to set the initial state, you should either use builder, or the aptly-name 'default'.

    has 'foo' => ( builder => 'build_foo', ); sub build_foo { my $self = shift; return 'blah'; } # OR has 'foo' => ( default => sub { my $self = shift; return 'blah'; }, );

    We're not surrounded, we're in a target-rich environment!
Re^3: Moose - nalia
by stvn (Monsignor) on Mar 22, 2008 at 15:19 UTC
    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
      Thanks again stvn,

      You are too gracious, and too modest!

      I see now exactly why builder was not doing what I needed.

      Of course, if the attributes are not marked as 'lazy', then that insta +nce 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'.
      Thanks so much for the clarification and help. I was uncomfortable using trigger for initialization although it served my immediate needs, and will switch to builder with the use of 'lazy'. Thanks for divining my needs.

      My clients and I thank you for this very useful software. I just rewrote and cleaned up a module that was getting unwieldy. Moose does exaclty what good software should do; it allows me to work on higher order problems. And it makes for cleaner and more extensible software. It also allows for rapid development. I rewrote in a couple of mellow days what would have taken much longer and I have all the features that I need for rapid, reliable dev at my fingertips. Excellent work.

      Also, thanks for the advice on overriding &new. Not an advantageous use of Moose.

      One more question. Any advice on test driven development in Moose? Any preferred testing idioms? Any affordances or most optimal leveraging of Moose and testing idioms?

        Any advice on test driven development in Moose? Any preferred testing idioms? Any affordances or most optimal leveraging of Moose and testing idioms?

        I write my tests with Moose the same way I wrote them without Moose actually, using Test::More and friends. We do have a Test::Moose module included in the distro which provides a few helpers, it is a relatively new module too, so any ideas for additional testing functions are welcome.

        The only real difference between my pre-Moose tests and the tests I write now is that I am no longer testing things like accessors or object creation or correct type usage, cause I know that Moose tests all that for me. The Moose test suite currently has 2926 test across 133 files, and the Class::MOP test suite has 1971 test across 56 files, in addition to this we regularly smoke test the MooseX:: modules in our svn repository which adds another thousand tests or so. Because of this, I feel pretty confident that I only need to test what it is my module does, and not that the OO is working correctly. This is not unlike writing tests in a compiled and statically typed language like Java, Haskell or OCaml, in those languages the compiler will do a kind of sanity check on the code, leaving you to just test what it is your code actually does.

        -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://675542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-09-15 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls