我想做的是:
我正在编写一个 perl Moose 类,我希望有一个类属性,它是一个哈希值,并在构建时初始化为默认值。
我的尝试:
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;
}
Run Code Online (Sandbox Code Playgroud)
总的来说,我对 Moose 和 Perl 很陌生,如果我错过了文档中的某些内容,请原谅。
Moose没有定义Hash为类型(请参阅Moose::Manual::Types)。
但它定义了HashRef。为了使用它,请将构建器的最后一行更改为
return \%hash
Run Code Online (Sandbox Code Playgroud)
并将类型约束更改为
isa => 'HashRef',
Run Code Online (Sandbox Code Playgroud)
它仍然定义实例属性,而不是类属性。要定义类属性,请使用MooseX::ClassAttribute。