使用 Moose,如何设置“ro”的值?属性 Trait,在运行时?
我有一个属性特征,我想在其他类属性的基础上设置它。我真的想要一个属性特征的默认值,它获取类 $self
的副本,而不是属性的元。我想在我的班级中做这样的事情:
after 'BUILD' => sub {
my $self = shift;
$self->meta->get_attribute('id')->column_name( $self->_unique_key_name );
};
但是,我想保留我的属性特质RO?这可能吗。我知道 MOP 允许设置类属性的值,但我不知道如何在元属性上设置属性。
I've got an Attribute Trait that I want to set on the basis of other class attributes. I realy want a default on an Attribute Trait that gets a copy of the class $self
and not the meta for the attribute. I want to do something like this in my class:
after 'BUILD' => sub {
my $self = shift;
$self->meta->get_attribute('id')->column_name( $self->_unique_key_name );
};
But, I want to keep my attribute trait RO? Is this possible. I know the MOP allows one to set the value of a class-attribute, but I can't figure out how to set an attribute on the meta-attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来像是一个非常奇怪的设计(为什么元类需要它所描述的类的实例?)——但是您可以通过使用元类的元类轻松地做到这一点(请记住,Moose 元类是使用 MOP 引导的本身):
还请记住,如果默认值本身是引用,则需要将它们包装在 coderef 中:
$some_value = sub { $instance };
实际上,这不起作用 - 'default' 是只读的。我建议您重新考虑您的设计,而不是在如此低的级别上摆弄 MOP - 例如,将您的“默认”存储在另一个属性中,并编写一个委托给它的默认子:
This sounds like a really odd design (why would a metaclass need an instance of a class it is describing?) -- but you can do this easily enough by using the metaclass of the metaclass (remember that Moose meta classes are bootstrapped using the MOP itself):
Also remember that defaults need to be wrapped in a coderef if they are references themselves:
$some_value = sub { $instance };
Actually, this won't work - 'default' is read only. Rather than fiddling with the MOP at such a low level, I would urge you to reconsider your design - e.g. store your 'default' in another attribute, and writing a default sub that delegated to it: