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.
But this question arose because sometimes a MooseX module doesn't have an equivalent in the MouseX namespace, and I found that I could s开发者_Python百科till 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
?
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.
精彩评论