I understand that using builder
enables subclasses to override attribute defaults easily and roles can require
them. This can also be accomplished using default
like so:
has 'foo' =>
is => 'rw',
isa => 'Str',
default => sub { $_[0]->_build_foo };
I'm wondering if there are further advantages to using builder
I'm not aware of? I've come up with some myself:
builder
is declarative so you can introspect that foo
is built by _build_foo
builder
eliminates a subroutine wrapper making it a bit fasterbuilder
allows the use of the helpful lazy_build
.UPDATE To clarify, this isn't about default
vs builder
in general but default => sub { $_[0]->_build_foo }
vs builder => '_build_foo'
.
I think you've already answered your own question. Using builder
allows late-binding, which plays nicely with roles and classes that are intended to be subclassed. It's also valuable if the builder is pretty long — I never put a default
more than a line long into an attribute definition. There's no real functional difference; default
can easily emulate builder
, but the result isn't very pretty.