我想做的是以下幾點:
我正在撰寫一個 perl Moose 類,我想要一個類屬性,它是一個 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 的新手,如果我遺漏了檔案中的某些內容,請原諒。
uj5u.com熱心網友回復:
Moose沒有定義Hash為型別(參見Moose::Manual::Types)。
但是,它定義了HashRef。為了使用它,將構建器的最后一行更改為
return \%hash
并將型別約束更改為
isa => 'HashRef',
它仍然定義實體屬性,而不是類屬性。要定義類屬性,請使用MooseX::ClassAttribute。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498062.html
上一篇:二維陣列列印作為參考
