驼鹿& isa 数组引用

发布于 2024-11-09 22:12:55 字数 1211 浏览 0 评论 0原文

我正在尝试将 Moose 与 Moose::Meta::Attribute::Native::Trait::Array 一起使用,但看起来 ArrayRef 助手对我不起作用。下面是我的代码,它返回

Can't call method "add_item" on unblessed reference at bug.pl line 42.

我使用 Moose 2.0007 和 Perl v5.10.1。 Moose::Autobox 已安装。 我将不胜感激任何建议。

#!/usr/bin/perl

use strict;

package CycleSplit;
use Moose;
has 'name'=>(isa=>'Str', is=>'rw');
has 'start'=>(isa=>'Num', is=>'rw');
has 'length'=>(isa=>'Num', is=>'rw');
1;

package Cycle;
use Moose;
my @empty=();
has 'name' => (isa => 'Str', is => 'rw');
has 'splits' => (
    traits  => ['Array'],
    isa=>'ArrayRef[CycleSplit]',
    is => 'rw',
    default=>sub { [] },
    handles=>{
        add_item=>'push',
    },
);


no Moose;
1;

package Main;

sub Main {
    my $cyc=Cycle->new();
    $cyc->name("Days of week");

    for my $i (1..7) {
        my $spl=CycleSplit->new();
        $spl->name("Day $i");
        $spl->start($i/7-(1/7));
        $spl->length(1/7);
        $cyc->splits->add_item($spl);
    }

    my $text='';
    foreach my $spl ($cyc->splits) {
        $text.=$spl->name." ";
    }

    print $text;
}

Main;

I am trying to use Moose with Moose::Meta::Attribute::Native::Trait::Array but it looks like ArrayRef helper doesn't work for me. Bellow is my code which returns

Can't call method "add_item" on unblessed reference at bug.pl line 42.

I use Moose 2.0007 and Perl v5.10.1. Moose::Autobox is installed.
I would appreciate any suggestion.

#!/usr/bin/perl

use strict;

package CycleSplit;
use Moose;
has 'name'=>(isa=>'Str', is=>'rw');
has 'start'=>(isa=>'Num', is=>'rw');
has 'length'=>(isa=>'Num', is=>'rw');
1;

package Cycle;
use Moose;
my @empty=();
has 'name' => (isa => 'Str', is => 'rw');
has 'splits' => (
    traits  => ['Array'],
    isa=>'ArrayRef[CycleSplit]',
    is => 'rw',
    default=>sub { [] },
    handles=>{
        add_item=>'push',
    },
);


no Moose;
1;

package Main;

sub Main {
    my $cyc=Cycle->new();
    $cyc->name("Days of week");

    for my $i (1..7) {
        my $spl=CycleSplit->new();
        $spl->name("Day $i");
        $spl->start($i/7-(1/7));
        $spl->length(1/7);
        $cyc->splits->add_item($spl);
    }

    my $text='';
    foreach my $spl ($cyc->splits) {
        $text.=$spl->name." ";
    }

    print $text;
}

Main;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赠佳期 2024-11-16 22:12:55

handles 将方法添加到类本身,而不是属性。另一个问题是 splits 属性仍然是 arrayref,因此您需要在几秒钟内取消引用 foreach。更正后的代码如下:

sub Main {
    my $cyc=Cycle->new();
    $cyc->name("Days of week");

    for my $i (1..7) {
        my $spl=CycleSplit->new();
        $spl->name("Day $i");
        $spl->start($i/7-(1/7));
        $spl->length(1/7);
        $cyc->add_item($spl);               # removed splits
    }

    my $text='';
    foreach my $spl (@{ $cyc->splits }) {   # added array dereference
        $text.=$spl->name." ";
    }

    print $text;
}

handles add method to class itself, not to the attribute. Another problem is that splits attribute is still arrayref, so you need dereference in seconds foreach. Corrected code is as follows:

sub Main {
    my $cyc=Cycle->new();
    $cyc->name("Days of week");

    for my $i (1..7) {
        my $spl=CycleSplit->new();
        $spl->name("Day $i");
        $spl->start($i/7-(1/7));
        $spl->length(1/7);
        $cyc->add_item($spl);               # removed splits
    }

    my $text='';
    foreach my $spl (@{ $cyc->splits }) {   # added array dereference
        $text.=$spl->name." ";
    }

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