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:
builderis declarative so you can introspect thatfoois built by_build_foobuildereliminates a subroutine wrapper making it a bit fasterbuilderallows the use of the helpfullazy_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
builderallows 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 adefaultmore than a line long into an attribute definition. There’s no real functional difference;defaultcan easily emulatebuilder, but the result isn’t very pretty.