下面的代码定义了两个类(DeckA和DeckB),它们的区别仅在于它们是否使用
MooseX::AttributeHelpers附带的功能.Moose为DeckB生成的getter不是我所期望的.这是一个错误还是我误解了
MooseX::AttributeHelpers和
MooseX::FollowPBP应该如何互动?
我现在的解决方法是避免在这种情况下使用is参数,而是根据需要声明读者和编写者.
use strict; use warnings; my %moose_args = ( isa => 'ArrayRef[Str]',is => 'ro',default => sub {[]},); my %moose_attr_helper_args = ( Metaclass => 'Collection::Array',provides => { elements => 'get_all_cards',},); package DeckA; use Moose; use MooseX::FollowPBP; use MooseX::AttributeHelpers; has 'cards' => (%moose_args); package DeckB; use Moose; use MooseX::FollowPBP; use MooseX::AttributeHelpers; has 'cards' => (%moose_args,%moose_attr_helper_args); package main; for my $class (qw(DeckA DeckB)){ my $deck = $class->new; print "\n$class\n"; for my $method ( qw(cards get_cards get_all_cards) ){ print "$method: ",$deck->can($method) ? 'yes' : 'no',"\n"; } }
输出:
DeckA cards: no get_cards: yes get_all_cards: no DeckB cards: yes # Not what I expected. get_cards: no # Not what I expected. get_all_cards: yes