perl - Using Moose, how do I set the value of a 'ro' Attribute Trait, in runtime? -
i've got attribute trait want set on basis of other class attributes. realy want default on attribute trait gets copy of class $self
, not meta attribute. want in class:
after 'build' => sub { $self = shift; $self->meta->get_attribute('id')->column_name( $self->_unique_key_name ); };
but, want keep attribute trait ro? possible. know mop allows 1 set value of class-attribute, can't figure out how set attribute on meta-attribute.
this sounds odd design (why metaclass need instance of class describing?) -- can enough using metaclass of metaclass (remember moose meta classes bootstrapped using mop itself):
$self->meta->meta->get_attribute("foo")->default($some_value);
also remember defaults need wrapped in coderef if references themselves: $some_value = sub { $instance };
actually, won't work - 'default' read only. rather fiddling mop @ such low level, urge reconsider design - e.g. store 'default' in attribute, , writing default sub delegated it:
package myapp::meta::attribute::trait::foo; # set @ runtime, when have instance store here has _default_of_foo => ( => 'rw', isa => 'object', ); has foo => ( => 'ro', isa => 'object', lazy => 1, default => sub { shift->_default_of_foo }, );
Comments
Post a Comment