3
如何設置Moose只讀屬性特徵?Moose只讀屬性特徵以及如何設置它們?
package AttrTrait;
use Moose::Role;
has 'ext' => (isa => 'Str', is => 'ro');
package Class;
has 'foo' => (isa => 'Str', is => 'ro', traits => [qw/AttrTrait/]);
package main;
my $c = Class->new(foo => 'ok');
$c->meta->get_attribute('foo')->ext('die') # ro attr trait
什麼是閱讀的目的只有屬性特質,如果你不能將其設置在構造函數或運行時?有什麼我在Moose::Meta::Attribute失蹤?有沒有辦法使用meta
進行設置?
$c->meta->get_attr('ext')->set_value('foo') # doesn't work either (attribute trait provided not class provided method)