MouseX::Types(3pm)
π MouseX::Types β the manual page, but make it cute π
Okay bestie, listen. This is the manual for MouseX::Types (that's the MouseX::Types(3pm) page, yes ma'am), and it's literally just a way to organize your Mouse types into libraries. Like, instead of scattering type definitions everywhere like confetti at a gender reveal, you put them all in one tidy little library package. Iconic behavior. β¨
π¦ SYNOPSIS π¦
πΈ Library Definition πΈ
So first, you build the library.</purgle> Predeclare your own types, import the builtin ones, define subtypes, add coercions, done. Here's the whole vibe, verbatim:
package MyLibrary;
# predeclare our own types
use MouseX::Types
-declare => [qw(
PositiveInt NegativeInt
)];
# import builtin types
use MouseX::Types::Mouse 'Int';
# type definition.
subtype PositiveInt,
as Int,
where { $_ > 0 },
message { "Int is not larger than 0" };
subtype NegativeInt,
as Int,
where { $_ < 0 },
message { "Int is not smaller than 0" };
# type coercion
coerce PositiveInt,
from Int,
via { 1 };
1;
Notice how subtype gets as, where, and message? And coerce gets from and via? That's the whole recipe, bestie. Slay. π
π Usage π
Now you actually use it. Pull the constants into your class and they're type names you can hand to isa. Also, free helper functions show up for checking and coercing:
package Foo;
use Mouse;
use MyLibrary qw( PositiveInt NegativeInt );
# use the exported constants as type names
has 'bar',
isa => PositiveInt,
is => 'rw';
has 'baz',
isa => NegativeInt,
is => 'rw';
sub quux {
my ($self, $value);
# test the value
print "positive\n" if is_PositiveInt($value);
print "negative\n" if is_NegativeInt($value);
# coerce the value, NegativeInt doesn't have a coercion
# helper, since it didn't define any coercions.
$value = to_PositiveInt($value) or die "Cannot coerce";
}
1;
And yeah β notice the comment up there. NegativeInt doesn't get a to_NegativeInt helper because it never defined any coercions. Only the types with coercions get the to_ glow-up. Periodt. β¨
π«Ά No cap: for each declared type you get anis_TypeNamecheck helper, and ato_TypeNamecoercion helper only if that type actually declared coercions.
π AUTHORS π
The legends behind this:
- Kazuhiro Osawa β
yappo <at> shibuya <dΓΆt> pl - Shawn M Moore
- tokuhirom
- Goro Fuji
β¦and, in their own words, with plenty of code borrowed from MooseX::Types. Respect where it's due. πβοΈ
π REPOSITORY π
Wanna clone it and poke around? Do that:
git clone git://github.com/yappo/p5-mousex-types.git MouseX-Types
β SEE ALSO β
MouseπMooseX::Typesπ¦
π COPYRIGHT AND LICENSE π
Legal stuff, but make it cute:
Copyright (c) 2008-2010, Kazuhiro Osawa and partly based on MooseX::Types, which is (c) Robert Sedlacek.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
πΈ Translation: it's Perl-licensed, so go forth and remix, bestie.
And that's the whole page, fr fr. This gem is from perl v5.34.0, dated 2022-06-16 β the MouseX::Types(3pm) manual, and honestly? Ate. π π¦ π β¨
π¦ where this page comes from
- debian bookworm: libmousex-types-perl 0.06-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmousex-types-perl 0.06-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmousex-types-perl 0.06-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmousex-types-perl 0.06-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmousex-types-perl 0.06-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms