Perl Moose,如何初始化哈希的实例属性

发布于 2025-02-12 02:59:52 字数 658 浏览 1 评论 0原文

我要做的是以下内容:

我正在写一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2025-02-19 02:59:52

moose 并未将hash定义为一种类型(请参阅 Moose :: Manual ::类型)。

但是,它定义了hashref。为了使用它,请将构建器的最后一行更改

return \%hash

为“类型约束

isa => '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 to

return \%hash

and change the type constraint to

isa => 'HashRef',

It still defines an instance attribute, not a class attribute. To define class attributes, use MooseX::ClassAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文