Re: builder vs initializer
[email protected] (Yuval Kogman)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
The initializer is always used at new().
the builder is used if no value is given to create a value, but then
the value still passes through the initializer:
use Moose
sub init { warn "init @_" }
sub build { warn "build @_" }
has foo => ( initializer => "init", builder => "build" )
If we provide a value, then only the initializer is called:
__PACKAGE__->new( foo => "an arg" );
outputs:
init Class=HASH(0x813ad0) an arg CODE(0x8fa870) Moose::Meta::Attribute=HASH(0x8ea390)
And here we let the builder provide the value:
__PACKAGE__->new;
outputs:
build Class=HASH(0x813710)
init Class=HASH(0x813710) 1 CODE(0x8fabe0) Moose::Meta::Attribute=HASH(0x8ea390)
Note that the 1 is the return value from 'warn "build @_"'
I hope this clarifies things, feel free to submit a doc patch if you
think it can be improved
Cheers
On Sat, Apr 19, 2008 at 21:20:42 -0700, Ian Tegebo wrote:
> When does one want a builder of an attribute, vs an initalizer? I'm
> not sure what the difference is:
>
> Description of 'builder' from Class::MOP::Attribute,
>
> [...] This should be a method in the class associated with the
> attribute, not a method in the attribute class itself.
>
> Does this mean a method in My::Class vs one in Class::MOP::Attribute
> or some subclass thereof?
>
> And then in the description of 'initializer' from Class::MOP::Attribute,
>
> [...] The initializer is used to set the attribute value on an
> instance when the attribute is set during instance initialization.
> [...]
>
> Does the 'builder' _not_ set the value during instance initialization?
>
>
> --
> Ian Tegebo
--
Yuval Kogman <[email protected]>
http://nothingmuch.woobling.org 0xEBD27418