在其他角色上实施要求不起作用?

发布于 2024-12-01 13:26:57 字数 1749 浏览 2 评论 0原文

我的第一个角色如下:

package AccBack::RTransaction;

use strict;
use warnings;

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

requires "_log";
requires "_config";

我的第二个角色实现了第一个角色,如下:

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::RAccounting”需要实现“_config”方法 通过“AccBack::会员资格” C:/strawberry/perl/site/lib/Moose/Meta/Role/Application/ToCla

我不明白问题出在哪里。它与 http:// 相同search.cpan.org/~doy/Moose-2.0203/lib/Moose/Cookbook/Roles/Recipe1.pod

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

My first Role is the following one:

package AccBack::RTransaction;

use strict;
use warnings;

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

requires "_log";
requires "_config";

My second Role, which implement the first Role, is the following one:

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

My base class is the following one:

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

If I wan't to start my program, I get this error:

'AccBack::RAccounting' requires the method '_config' to be implemented
by 'AccBack::Membership' at
C:/strawberry/perl/site/lib/Moose/Meta/Role/Application/ToCla

I don't understand where the problem is. It is the same things as http://search.cpan.org/~doy/Moose-2.0203/lib/Moose/Cookbook/Roles/Recipe1.pod.

Does anybody have an idea about what I've misunderstand?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-12-08 13:26:57

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

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

存根子方法将满足要求,但不会妨碍角色应用。

This is a known problem that will hopefully be fixed in the future. In the meantime, you should be able to satisfy the requirement in the second role by adding a stub method like this:

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

The stub sub will fulfill the requirement, but will not get in the way of role application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文