我正在尝试使用Moose :: Meta :: Attribute :: Native :: Trait :: Array,但它看起来像ArrayRef helper不适用于我。贝娄是我的代码返回Moose&isa ArrayRef
Can't call method "add_item" on unblessed reference at bug.pl line 42.
我用驼鹿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;
不是驼鹿专家,但尝试:$ cyc-> add_item($ spl); – snoofkin 2011-05-27 10:45:03