Moose::Manual::Attributes states:
As an alternative to using a subroutine reference [for default], you can instead supply a builder method for your attribute: ... This has several advantages. First, it moves a chunk of code to its own named method, which improves readability and code organization.
So, your attribute could define a default thusly:
has attr => (
is => 'ro',
builder => 'subroutine'
);
sub subroutine {
# figure out and return default value
}
I don't get why this has to be separate from default. Couldn't you just pass a reference to a named subroutine?
has attr => (
is => 'ro',
default => \&subroutine
);
And would that not be better programming practice, since you're guaranteed not to accidentally refer to a subroutine that doesn't exist? You'd be referring to the method with a logical reference instead of a symbolic reference.