Re: Issue with HashRef attritubes

[email protected] (Chris Weyl) Thu, 27 Jun 2013 14:30:03 -0700 (PDT)
Newsgroups perl.moose
Message-ID <alpine.DEB.2.02.1306271423420.13039@athena>
On Thu, 27 Jun 2013, Mark Allen wrote:

> I typically see classes with delegated methods use a
>
> default => sub { {} }, 

Or better*:

    # with this somewhere above: use MooseX::AttributeShortcuts;
    builder => sub { { } },

...as defaults tend to be a pain when trying to override or otherwise 
alter them, but this syntax causes a builder method with the appropriate 
name (e.g. lazy_build style, _build_$name()) to be generated and used. 
(Allowing for subclasses to override, or roles, or method modifiers, etc, 
etc).

e.g.

     use MooseX::AttributeShortcuts;
     has foo => (is => 'ro', lazy => 1, builder => sub { 'bar' });

is effectively the same as

     has foo => (is => 'ro', lazy => 1, builder => '_build_foo');
     sub _build_foo { 'bar' }

                      -Chris

* for values of "better" that correspond to this author's opinions.