【问题标题】:Implement requires on other Role doesn't work?对其他角色实施要求不起作用?
【发布时间】:2011-08-25 16:35:16
【问题描述】:

我的第一个角色如下:

package AccBack::RTransaction;

use strict;
use warnings;

use Moose::Role;
use MooseX::Method::Signatures;

requires "_log";
requires "_config";

我的第二个 Role 实现了第一个 Role,如下所示:

package AccBack::RAccounting;

use AccBack::RTransaction;

requires "_log";

has "_config"            => (
    isa         => "Accounting::Config",
    is          => "ro",
    lazy        => 1,
    default     => sub { return Accounting::Config->new(); }
);

has "fibu"              => (
    isa         => "Maybe[Accounting::Fibu]",
    is          => "rw",
    writer      => "setFibu",
    reader      => "getFibu",
    default     => undef,
);

with "AccBack::RTransaction";

我的基类如下:

package AccBack::Membership;

use AccBack::RAccounting;

has "_log"              => (
    isa         => "Log::Log4perl::Logger",
    is          => "ro",
    default     => sub { 
        return Log::Log4perl->get_logger("AccBack::Membership");
    }
);

has "mailMergeOption"  => (
    isa         => "Maybe[HashRef]",
    is          => "rw",
    writer      => "setMailMergeOption",
    reader      => "getMailMergeOption",
    default     => undef,
);

# Roles
with "AccBack::RAccounting";

如果我不想启动我的程序,我会收到以下错误:

'AccBack::RAAccounting' 需要实现方法 '_config' 由“AccBack::Membership”在 C:/strawberry/perl/site/lib/Moose/Meta/Role/Application/ToCla

我不明白问题出在哪里。和http://search.cpan.org/~doy/Moose-2.0203/lib/Moose/Cookbook/Roles/Recipe1.pod是一样的。

有人知道我误解了什么吗?

【问题讨论】:

  • 只是一个猜测,但如果将with 语句放在顶部而不是底部,它是否有效?
  • 不,那将保证它不起作用; 'has' 和 'with' 都是正常的运行时函数调用。

标签: perl moose role


【解决方案1】:

这是一个已知问题,希望在未来得到解决。同时,您应该能够通过添加这样的存根方法来满足第二个角色的要求:

sub _config;
has "_config"            => (
    isa         => "Accounting::Config",
    is          => "ro",
    lazy        => 1,
    default     => sub { return Accounting::Config->new(); }
);

存根子会满足要求,但不会妨碍角色申请。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2017-07-12
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多