Perl Moose,如何初始化哈希的实例属性
我要做的是以下内容:
我正在写一个perl驼鹿类,我希望ot具有一个hash的类属性,并在构建时被初始化为默认值。
我的尝试:
has sweep_prop_configuration => (
is=>'rw',
isa => 'Hash',
reader => 'sweep_prop_configuration',
writer => '_sweep_prop_configuration',
builder => '_build_sweep_prop_configuration',
predicate => 'has_sweep_prop_configuration',
);
sub _build_sweep_prop_configuration {
my $self = shift;
my %hash;
$hash{point_number}=0;
$hash{number_of_sweep}=0;
$hash{backwards}=-1;
$hash{at_end}=-1;
$hash{at_end_val}=0;
$hash{save_all}=-1;
return %hash;
}
我是Moose和Perl的新手,如果我错过了文档中的某些内容,请原谅。
What I am tring to do is the following:
I am writing a perl Moose Class and I want ot have a class attribute that is an Hash and is initialized to default values upon building.
My attempt:
has sweep_prop_configuration => (
is=>'rw',
isa => 'Hash',
reader => 'sweep_prop_configuration',
writer => '_sweep_prop_configuration',
builder => '_build_sweep_prop_configuration',
predicate => 'has_sweep_prop_configuration',
);
sub _build_sweep_prop_configuration {
my $self = shift;
my %hash;
$hash{point_number}=0;
$hash{number_of_sweep}=0;
$hash{backwards}=-1;
$hash{at_end}=-1;
$hash{at_end_val}=0;
$hash{save_all}=-1;
return %hash;
}
I am new to Moose and perl in general, excuse me if I missed something in the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
moose 并未将
hash
定义为一种类型(请参阅 Moose :: Manual ::类型)。但是,它定义了
hashref
。为了使用它,请将构建器的最后一行更改为“类型约束
”仍然定义了实例属性,而不是类属性。要定义类属性,请使用 moosex :: classattribute 。
Moose doesn't define
Hash
as a type (see Moose::Manual::Types).It defines
HashRef
, though. In order to use it, change the builder's last line toand change the type constraint to
It still defines an instance attribute, not a class attribute. To define class attributes, use MooseX::ClassAttribute.