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_AttributesAndSubclassing
  • Moose::Util::TypeConstraints
  • Moose::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>

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