【发布时间】:2014-05-03 14:07:57
【问题描述】:
是否可以为 moose 属性提供访问器包装器,而不必每次都编写它?
示例: * 有一个 TkRef 类型的属性 * 它应该提供一个用于设置值的包装器 * 定义属性时要定义包装器的名称 * 我不想写包装器
我想是这样的:
has _some_val => (
is => 'rw',
isa => 'TkRef',
coerce => 1,
init_arg => 'my_accessor_wrapper_name',
default => 'default value'
);
# Later in the class:
sub some_public_method {
my $self = shift;
# will set _some_val behind the scenes:
$self->my_accessor_wrapper_name('this will be the new value');
...
}
【问题讨论】:
-
您希望 Moose 创建用于设置和获取属性的子对象?!这正是它默认所做的。
标签: perl attributes moose accessor