使用Moops和Moose时,属性访问器不满足角色要求

时间:2014-02-03 13:23:10

标签: perl moose moops

请考虑以下代码示例:

use Moops;

role RoleA
using Moose {
  requires 'm1';
  method m2() {
    $self->m1." World!\n";
  }
}

role RoleB
using Moose {
  has 'm1' => ( accessor => 'm1', is => 'ro', isa => 'Str', default => 'Hello');
}

class ClassA
with RoleB
with RoleA
using Moose {
  method m3() {
    $self->m2();
  }
}

print ClassA->new()->m3();

“编译”失败,因为属性访问器似乎不满足m1的角色请求:

$ perl roletest.pl 
'RoleB|RoleA' requires the method 'm1' to be implemented by 'ClassA' at /usr/lib/perl5/site_perl/Moose/Meta/Role/Application/ToClass.pm line 134.
    Moose::Meta::Role::Application::ToClass::check_required_methods('Moose::Meta::Role::Application::ToClass=HASH(0x28b9d00)', 'Moose::Meta::Role::Composite=HASH(0x28c6270)', 'Moose::Meta::Class=HASH(0x28ee7c0)') called at /usr/lib/perl5/site_perl/Moose/Meta/Role/Application.pm line 55
    Moose::Meta::Role::Application::apply('Moose::Meta::Role::Application::ToClass=HASH(0x28b9d00)', 'Moose::Meta::Role::Composite=HASH(0x28c6270)', 'Moose::Meta::Class=HASH(0x28ee7c0)') called at /usr/lib/perl5/site_perl/Moose/Meta/Role/Application/ToClass.pm line 36
    Moose::Meta::Role::Application::ToClass::apply('Moose::Meta::Role::Application::ToClass=HASH(0x28b9d00)', 'Moose::Meta::Role::Composite=HASH(0x28c6270)', 'Moose::Meta::Class=HASH(0x28ee7c0)', 'HASH(0x28e3410)') called at /usr/lib/perl5/site_perl/Moose/Meta/Role.pm line 470
    Moose::Meta::Role::apply('Moose::Meta::Role::Composite=HASH(0x28c6270)', 'Moose::Meta::Class=HASH(0x28ee7c0)') called at /usr/lib/perl5/site_perl/Moose/Util.pm line 163
    Moose::Util::_apply_all_roles('Moose::Meta::Class=HASH(0x28ee7c0)', undef, 'RoleB', 'RoleA') called at /usr/lib/perl5/site_perl/Moose/Util.pm line 99
    Moose::Util::apply_all_roles('Moose::Meta::Class=HASH(0x28ee7c0)', 'RoleB', 'RoleA') called at /usr/lib/perl5/site_perl/Moose.pm line 66
    Moose::with('Moose::Meta::Class=HASH(0x28ee7c0)', 'RoleB', 'RoleA') called at /usr/lib/perl5/site_perl/Moose/Exporter.pm line 409
    Moose::with('RoleB', 'RoleA') called at roletest.pl line 16
    main::BEGIN() called at roletest.pl line 23
    eval {...} called at roletest.pl line 23
BEGIN failed--compilation aborted at roletest.pl line 23.

使用普通MooseMoose::Roleworks(记录为here)。另外,请勿使用Moose后端works to

我已经尝试切换with语句的顺序无济于事。用方法声明替换属性可以解决问题,但我需要一种方法来指定属性要求。

有没有办法让这个工作?

1 个答案:

答案 0 :(得分:1)

你确定它适用于普通的驼鹿吗?因为我已经尝试了这个并且它失败并出现一个非常相似的错误消息:

use v5.14;
use strict;
use warnings;

package RoleA {
    use Moose::Role;
    requires 'm1';
    sub m2 {
        my $self = shift;
        $self->m1." World!\n";
    }
}

package RoleB {
    use Moose::Role;
    has 'm1' => ( accessor => 'm1', is => 'ro', isa => 'Str', default => 'Hello');
}

package ClassA {
    use Moose;
    with qw/ RoleB RoleA /;
    sub m3 {
        my $self = shift;
        $self->m2();
    }
}

print ClassA->new()->m3();

Moose(以及测试套件中相应的TODO测试用例)中存在许多问题,这些问题围绕着在其他角色中不满足requires的角色中的属性。例如,bug RT#77406类似(虽然不完全相同)。

UPDATE 通过一次一个地手动编写角色,按特定顺序,您可以使其工作:

use Moops;

role RoleA using Moose {
  requires 'm1';
  method m2() {
    $self->m1." World!\n";
  }
}

role RoleB using Moose {
  has 'm1' => ( is => 'ro', isa => 'Str', default => 'Hello' );
}

class ClassA using Moose {
  with 'RoleB';
  with 'RoleA';
  method m3() {
    $self->m2();
  }
}

print ClassA->new()->m3();