可以将 MooseX 模块与 Mouse 类一起使用吗?
我意识到这对于所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Mouse
的目的是可以访问Moose
的大部分功能,同时消除其昂贵的启动时间和类似 Yggdrasil 的依赖树。如果您使用MooseX
模块,该模块会引入Moose
,或者至少引入Moose::Exporter
/Moose ::Role
,然后您就消除了Mouse
的优势。观察:这么快!但随后:
太慢了。但是,如果这些启动时间对于您正在做的事情并不重要,那么您一开始就不应该打扰
Mouse
。Any::Moose
的存在是为了允许面向Moose
的模块使用Mouse
,除非Moose
已加载,在在这种情况下它只会使用它。The point of using
Mouse
is to have access to most features ofMoose
while eliminating its expensive startup time and Yggdrasil-like dependency tree. If you're using aMooseX
module with it, that module brings inMoose
, or at leastMoose::Exporter
/Moose::Role
, and you've then eliminated the benefits ofMouse
. Observe:So fast! But then:
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 aMoose
-oriented module to useMouse
unlessMoose
is already loaded, in which case it'll just use that.