tags:

views:

78

answers:

2

The following code defines two classes (DeckA and DeckB) that differ only in whether they use the features that come with MooseX::AttributeHelpers. The getters generated by Moose for DeckB are not what I expected. Is this a bug or am I misunderstanding how MooseX::AttributeHelpers and MooseX::FollowPBP ought to interact?

My workaround for now has been to avoid using the is argument in such situations and instead declare a reader and writer as needed.

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";
    }
}

Output:

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
+5  A: 

They don't work when you use the metaclass option for MX::AH.

However, the latest Moose has integrated support for native helpers, with a slightly tweaked API. This version uses traits (a role applied to the attribute), and it should work just fine with MX::FollowPBP.

Dave Rolsky
you beat me to this answer ... curse you Dave Rolsky ... CURSE YOUUUUU
perigrin
+1  A: 

I had the same problem, so I really appreciate FM's question and Dave Rolsky's answer.

Rephrasing part of his answer so that my simple self would have understood it the first time I read it:

Instead of using MooseX::AttributeHelpers, you can simply use "traits" in the latest version of Moose. This eliminates the conflict with MooseX::FollowPBP, while still giving you the same functionality.

For using traits, see Moose::Meta::Attribute::Native.

molecules