Perl: the Markov chain saw | |
PerlMonks |
Re: Confusion with Moo's builder attribute and a file handleby tobyink (Canon) |
on Jul 23, 2013 at 08:40 UTC ( [id://1045796]=note: print w/replies, xml ) | Need Help?? |
OK; I think you're confused about what builders are supposed to do. Your builder will only be executed if you do NOT provide a value for the base_file attribute.
Further, you seem to be assuming that within the builder sub, $_[0] is some sort of file name. It's not; it's $self. I think what you want is coercions...
In summary: use coercions to munge incoming values; use builders to provide a default value when there is no incoming value. Here's another way you could have done it... provide a base attribute which is the filename as a string, and then build the base_file attribute from that:
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
In Section
Seekers of Perl Wisdom
|
|