Moose::...k::Types(3)
β¨ NAME β¨
Okay bestie, so this is Moose::Cookbook::Snack::Types β literally just snippets of code for using Types and Type Constraints. It's a lil snack, get it? π (and it lives in the User Contributed Perl Documentation corner of the manual-verse, iykyk)
π VERSION π
version 2.4000 β yes queen, she's on 2.4000 π
πΈ SYNOPSIS πΈ
No thoughts, just vibes and code:
package Point;
use Moose;
has 'x' => ( isa => 'Int', is => 'ro' );
has 'y' => ( isa => 'Int', is => 'rw' );
package main;
use Try::Tiny;
my $point = try {
Point->new( x => 'fifty', y => 'forty' );
}
catch {
print "Oops: $_";
};
my $point;
my $xval = 'forty-two';
my $xattribute = Point->meta->find_attribute_by_name('x');
my $xtype_constraint = $xattribute->type_constraint;
if ( $xtype_constraint->check($xval) ) {
$point = Point->new( x => $xval, y => 0 );
}
else {
print "Value: $xval is not an " . $xtype_constraint->name . "\n";
}
π¦ DESCRIPTION π¦
So this is the Point example from Moose::Cookbook::Basics::Point_AttributesAndSubclassing β but make it type-checked. Same vibes, upgraded β¨
Here's the tea: if you try to assign a string value to an attribute that's an "Int", Moose will die with an explicit error message. Not a vague little "something went wrong" β no, she's specific π
. The error will include the attribute name, plus the type constraint name, plus the value that failed the constraint check. Receipts and everything.
We use Try::Tiny to catch this error message, so instead of your program just perishing you get to be like "oh okay noted π".
Later on, we grab the Moose::Meta::TypeConstraint object from a Moose::Meta::Attribute, and then use that Moose::Meta::TypeConstraint to check a value directly. Like, manually, ourselves, no constructor involved. Main character energy fr fr π
π SEE ALSO π
Go read these, bestie, they're the whole lore:
Moose::Cookbook::Basics::Point_AttributesAndSubclassingMoose::Util::TypeConstraintsMoose::Meta::Attribute
π AUTHORS π
The legends who built this. Respect the roster π«Ά
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Not me ending on the legal bit β but fr fr, free software, do what you want with it within those Perl 5 terms πΈ
Okay that's the whole page, mwah π This was perl v5.42.0, dated 2025-07-25 β and it's a snack, remember, not a meal. Go forth and type-check, bestie πβ¨ππ¦
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms