Type::Tiny::Manual::UsingWithMouse - how to use Type::Tiny with Mouse
First read Type::Tiny::Manual::Moo, Type::Tiny::Manual::Moo2, and Type::Tiny::Manual::Moo3. Everything in those parts of the manual should work exactly the same in Mouse.
This part of the manual will focus on Mouse-specifics.
Overall, Type::Tiny is less well-tested with Mouse than it is with Moose and Moo, but there are still a good number of test cases for using Type::Tiny with Mouse, and there are no known major issues with Type::Tiny's Mouse support.
Mouse does have a built-in type constraint system which is fairly convenient to use, but there are several reasons you should consider using Type::Tiny instead.
Type::Tiny provides helpful methods like where and plus_coercions that allow type constraints and coercions to be easily tweaked on a per-attribute basis.
where
plus_coercions
Something like this is much harder to do with plain Mouse types:
has name => ( is => "ro", isa => Str->plus_coercions( ArrayRef[Str], sub { join " ", @$_ }, ), coerce => 1, );
Mouse tends to encourage defining coercions globally, so if you wanted one Str attribute to be able to coerce from ArrayRef[Str], then all Str attributes would coerce from ArrayRef[Str], and they'd all do that coercion in the same way. (Even if it might make sense to join by a space in some places, a comma in others, and a line break in others!)
Type::Tiny provides automatic deep coercions, so if type Xyz has a coercion, the following should "just work":
isa xyzlist => ( is => 'ro', isa => ArrayRef[Xyz], coerce => 1 );
Type::Tiny offers a wider selection of built-in types.
By using Type::Tiny, you can use the same type constraints and coercions for attributes and method parameters, in Mouse and non-Mouse code.
TODO
Types::Standard should be a drop-in replacement for MooseX::Types. And Types::Common::Numeric and Types::Common::String should easily replace MouseX::Types::Common::Numeric and MouseX::Types::Common::String.
That said, if you do with to use a mixture of Type::Tiny and MouseX::Types, they should fit together pretty seamlessly.
use Types::Standard qw( ArrayRef ); use MouseX::Types::Mouse qw( Int ); # this should just work my $list_of_nums = ArrayRef[Int]; # and this my $list_or_num = ArrayRef | Int;
-mouse
mouse_type
Type::Tiny should run pretty much as fast as Mouse types do. This is because, when possible, it will use Mouse's XS implementations of type checks to do the heavy lifting.
There are a few type constraints where Type::Tiny prefers to do things without Mouse's help though, for consistency and correctness. For example, the Mouse XS implementation of Bool is... strange... it accepts blessed objects that overload bool, but only if they return false. If they return true, it's a type constraint error.
bool
Using Type::Tiny instead of Mouse's type constraints shouldn't make a significant difference to the performance of your code.
Here's your next step:
Type::Tiny::Manual::UsingWithClassTiny
Including how to Type::Tiny in your object's BUILD method, and third-party shims between Type::Tiny and Class::Tiny.
BUILD
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Type::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Type::Tiny
CPAN shell
perl -MCPAN -e shell install Type::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.