in reply to Moose: apply Role at Runtime (build vs. default) [solved]
Works for me!
use v5.14; use strict; use warnings; package MyObject { use Moose; } package MyRole { use Moose::Role; has myattr => ( is => 'ro', lazy => 1, builder => '_build_myattr', ); sub _build_myattr { "value"; } } my $obj = "MyObject"->new; Moose::Util::apply_all_roles($obj, "MyRole"); say $obj->myattr;
Also, whenever you call a builder method from a coderef, make sure you call it as a method so that subclassing works.
# Not this... default => \&_build_myattr, # This! default => sub { shift->_build_myattr(@_) },
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
In Section
Seekers of Perl Wisdom