如何通过 MooseX::Declare 声明一个类使用多个角色?

发布于 2024-07-26 23:19:46 字数 1164 浏览 9 评论 0原文

鉴于 Fooable 和 Barable 角色都已定义,那么如何说 FooBar 类负责 Fooable 和 Barable 呢? 我没有问题说

#!/usr/bin/perl

use MooseX::Declare;

role Fooable {
    method foo { print "foo\n" }
}

role Barable {
    method bar { print "bar\n" }
}

class Foo with Fooable {}
class Bar with Barable {}

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;

但是当我尝试添加时

class FooBar with Fooable, Barable {}

我得到了不太有用的错误

expected option name at [path to MooseX/Declare/Syntax/NamespaceHandling.pm] line 45

只是为了向自己证明我没有疯,我用 Moose 重写了它。 这段代码有效(但比罪恶更丑陋):

#!/usr/bin/perl

package Fooable;
    use Moose::Role;    
    sub foo { print "foo\n" }

package Barable;    
    use Moose::Role;    
    sub bar { print "bar\n" }

package Foo;    
    use Moose;    
    with "Fooable";

package Bar;    
    use Moose;    
    with "Barable";

package FooBar;    
    use Moose;    
    with "Fooable", "Barable";

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;
FooBar->new->foo;
FooBar->new->bar;

Given that the roles Fooable and Barable have both been defined, how do I say that class FooBar does Fooable and Barable? I have no problem saying

#!/usr/bin/perl

use MooseX::Declare;

role Fooable {
    method foo { print "foo\n" }
}

role Barable {
    method bar { print "bar\n" }
}

class Foo with Fooable {}
class Bar with Barable {}

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;

But when I try to add

class FooBar with Fooable, Barable {}

I get the less than useful error

expected option name at [path to MooseX/Declare/Syntax/NamespaceHandling.pm] line 45

Just to prove to myself that I am not crazy, I rewrote it using Moose. This code works (but is uglier than sin):

#!/usr/bin/perl

package Fooable;
    use Moose::Role;    
    sub foo { print "foo\n" }

package Barable;    
    use Moose::Role;    
    sub bar { print "bar\n" }

package Foo;    
    use Moose;    
    with "Fooable";

package Bar;    
    use Moose;    
    with "Barable";

package FooBar;    
    use Moose;    
    with "Fooable", "Barable";

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;
FooBar->new->foo;
FooBar->new->bar;

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

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

发布评论

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

评论(2

如何视而不见 2024-08-02 23:19:46

显然你需要括号:

#!/usr/bin/perl

use MooseX::Declare;

role Fooable {
    method foo { print "foo\n" }
}

role Barable {
    method bar { print "bar\n" }
}

class Foo with Fooable {}
class Bar with Barable {}
class FooBar with (Fooable, Barable) {}

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;
FooBar->new->foo;
FooBar->new->bar;

Apparently you need parentheses:

#!/usr/bin/perl

use MooseX::Declare;

role Fooable {
    method foo { print "foo\n" }
}

role Barable {
    method bar { print "bar\n" }
}

class Foo with Fooable {}
class Bar with Barable {}
class FooBar with (Fooable, Barable) {}

package main;

use strict;
use warnings;

Foo->new->foo;
Bar->new->bar;
FooBar->new->foo;
FooBar->new->bar;
风透绣罗衣 2024-08-02 23:19:46

请注意,这也有效:

class FooBar with Fooable with Barable {}

这似乎是我在 MooseX::Declare world 中看到的最常见用法。

另请注意,您也可以使用“经典”方式:

class FooBar {
    with qw(Fooable Barable);
}

在某些情况下需要这样做,因为这会立即组成角色,而在类行中定义角色会延迟到类块的末尾。

Just to note that this also works:

class FooBar with Fooable with Barable {}

This seems the most common usage I've seen in MooseX::Declare world.

Also note you can use the "classic" way also:

class FooBar {
    with qw(Fooable Barable);
}

There are cases where this is needed because this composes the role immediately whereas defining role in the class line gets delayed till the end of the class block.

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