Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Moose's builder take a string value?

Tags:

perl

moose

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.

like image 985
skiphoppy Avatar asked Feb 18 '09 16:02

skiphoppy


1 Answers

When the builder is called, this happens:

$object->$builder

If builder is a string (let's say build_attr), then users can write their own build_attr method in a subclass and it will be called. This makes the default value extensible via a simple named method mechanism.

If it's a reference to a subroutine, the reference is taken in the original class's package, which means it can't be overriden the same way.

like image 167
rjbs Avatar answered Sep 24 '22 01:09

rjbs