可以将 MooseX 模块与 Mouse 类一起使用吗?

发布于 2024-12-07 23:53:17 字数 732 浏览 0 评论 0原文

我意识到这对于所有 MooseX 模块来说通常是不可能的,特别是当模块深入研究 Moose 和 Mouse 不同的元类时。

但出现这个问题是因为有时 MooseX 模块在 MouseX 命名空间中没有等效模块,而我发现我仍然可以在我的 Mouse 类中使用 MooseX 模块。但我想一般性地问这个问题,即使有可用的 MouseX 等效版本(假设我懒得安装 MouseX,或者 MooseX 版本更新了新功能)。

例如,以下内容是有效的:

package Foo;
use Mouse;
use MooseX::Types::Common::Numeric 'PositiveInt';
has 'bar' => (
    is => 'rw',
    isa => PositiveInt,
);

当我查看 MouseX::Types::Common::Numeric 源时,它几乎是 MooseX::Types::Common::Numeric 的精确副本,尽管 MouseX::Types 存在差异,这是一个依赖项。由于它是 Perl 代码,因此使用 MouseX 模块也没有特别的性能优势。

因此,如果我们有一个 Mouse 类并可以选择使用等效的 MooseX 和 MouseX 模块,那么我们必须选择 MouseX 选项的原因是什么?为什么还要有 MouseX 等价物?

顺便说一句,我们应该如何与 Any::Moose 联系起来?

I realise that this is not generally possible for all MooseX modules, particularly where the module delves into the meta class where Moose and Mouse differ.

But this question arose because sometimes a MooseX module doesn't have an equivalent in the MouseX namespace, and I found that I could still use the MooseX module within my Mouse classes. But I want to ask this question in general, even if there is a MouseX equivalent available (let's say I'm too lazy to install the MouseX one, or the MooseX version is more recent with new features).

For example, the following is valid:

package Foo;
use Mouse;
use MooseX::Types::Common::Numeric 'PositiveInt';
has 'bar' => (
    is => 'rw',
    isa => PositiveInt,
);

When I looked into MouseX::Types::Common::Numeric source it was almost an exact copy of MooseX::Types::Common::Numeric, though there were differences in MouseX::Types which is a dependency. Since it is perl code there is no particular performance benefit in using the MouseX module either.

So if we have a Mouse class and a choice of using equivalent MooseX and MouseX modules, what reasons would we have to choose the MouseX option? Why have the MouseX equivalent anyway?

btw, how should we relate to this with Any::Moose?

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

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

发布评论

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

评论(1

笑,眼淚并存 2024-12-14 23:53:17

使用 Mouse 的目的是可以访问 Moose 的大部分功能,同时消除其昂贵的启动时间和类似 Yggdrasil 的依赖树。如果您使用 MooseX 模块,该模块会引入 Moose,或者至少引入 Moose::Exporter/Moose ::Role,然后您就消除了Mouse 的优势。观察:

rsimoes@desk-o-simoes:~$ time perl -MMouse -e 1

real    0m0.026s
user    0m0.020s
sys     0m0.000s

rsimoes@desk-o-simoes:~$ time perl -MMouse -MMouseX::Types::Common::Numeric -e 1

real    0m0.032s
user    0m0.030s
sys     0m0.000s

这么快!但随后:

rsimoes@desk-o-simoes:~$ time perl -MMoose -e 1

real    0m0.148s
user    0m0.120s
sys     0m0.020s

rsimoes@desk-o-simoes:~$ time perl -MMouse -MMooseX::Types::Common::Numeric -e 1

real    0m0.181s
user    0m0.150s
sys     0m0.020s

太慢了。但是,如果这些启动时间对于您正在做的事情并不重要,那么您一开始就不应该打扰 Mouse

Any::Moose 的存在是为了允许面向 Moose 的模块使用 Mouse,除非 Moose 已加载,在在这种情况下它只会使用它。

The point of using Mouse is to have access to most features of Moose while eliminating its expensive startup time and Yggdrasil-like dependency tree. If you're using a MooseX module with it, that module brings in Moose, or at least Moose::Exporter/Moose::Role, and you've then eliminated the benefits of Mouse. Observe:

rsimoes@desk-o-simoes:~$ time perl -MMouse -e 1

real    0m0.026s
user    0m0.020s
sys     0m0.000s

rsimoes@desk-o-simoes:~$ time perl -MMouse -MMouseX::Types::Common::Numeric -e 1

real    0m0.032s
user    0m0.030s
sys     0m0.000s

So fast! But then:

rsimoes@desk-o-simoes:~$ time perl -MMoose -e 1

real    0m0.148s
user    0m0.120s
sys     0m0.020s

rsimoes@desk-o-simoes:~$ time perl -MMouse -MMooseX::Types::Common::Numeric -e 1

real    0m0.181s
user    0m0.150s
sys     0m0.020s

So slow. But if those startup times don't matter for what you're doing, You shouldn't even be bothering with Mouse to begin with.

Any::Moose exists to allow a Moose-oriented module to use Mouse unless Moose is already loaded, in which case it'll just use that.

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