简体   繁体   English

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

[英]Is it ok to use MooseX modules with a Mouse class?

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. 我意识到所有MooseX模块通常都不可能,特别是当模块深入研究Moose和Mouse不同的元类时。

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. 但是这个问题出现了,因为有时MooseX模块在MouseX命名空间中没有等效物,我发现我仍然可以在我的Mouse类中使用MooseX模块。 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). 但是我想问一般这个问题,即使有一个MouseX等效物可用(假设我太懒了,不能安装MouseX,或者MooseX版本更新近有新功能)。

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. 当我查看MouseX::Types::Common::Numeric源时,它几乎是MooseX::Types::Common::Numeric的精确副本,尽管MouseX :: Types中存在差异,这是一种依赖。 Since it is perl code there is no particular performance benefit in using the MouseX module either. 由于它是perl代码,因此使用MouseX模块也没有特别的性能优势。

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? 因此,如果我们有一个Mouse类并且可以选择使用等效的MooseX和MouseX模块,那么我们选择MouseX选项的原因是什么? Why have the MouseX equivalent anyway? 为什么MouseX等价?

btw, how should we relate to this with Any::Moose ? 顺便问一下,我们应该如何与Any::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. 使用Mouse是访问Moose大多数功能,同时消除其昂贵的启动时间和类似Yggdrasil的依赖树。 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 . 如果您正在使用MooseX模块,那么该模块会MooseX Moose ,或至少Moose::Exporter / Moose::Role ,然后您就消除了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. 但是如果那些启动时间对你正在做的事情无关紧要,你甚至不应该开始使用Mouse

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. Any::Moose存在允许Moose定向模块使用Mouse除非已经加载了Moose ,在这种情况下它只会使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM