Moose::...nstraint(3)

πŸ’– NAME πŸ’–

Moose::Meta::TypeConstraint β€” bestie, this is the Moose Type Constraint metaclass, and she is serving looks. We're in the Moose::...nstraint(3) perl doc page btw, if the header didn't already spoil it. πŸŽ€


✨ VERSION ✨

version 2.4000 β€” that's the one, no cap.


🌸 DESCRIPTION 🌸

Okay so, imagine this class as one single type constraint. That's her whole job, that's her whole vibe. πŸ‘‘

Every type constraint in Moose β€” the built-in ones and the ones you write yourself β€” gets stored as an object of this class inside a Moose::Meta::TypeConstraint::Registry object. So yeah, they're all living in the registry together, iconic behavior honestly. πŸ¦‹


πŸ’… INHERITANCE πŸ’…

Moose::Meta::TypeConstraint is a subclass of Class::MOP::Object. Family tree slays, we love a legacy. 🌈


πŸ”₯ METHODS πŸ”₯

πŸ“ Moose::Meta::TypeConstraint->new(%options)

This makes a brand-new type constraint out of the %options you hand her. Here's the tea on every single one of those options:

  • name β€” the constraint's name. If you don't give her one, she'll just be like "fine, I'm __ANON__ now" and that's that. πŸ™ˆ
  • parent β€” a Moose::Meta::TypeConstraint object that acts as the parent type for the type you're creating. Totally optional, no pressure bestie.
  • constraint β€” the subroutine reference that does the actual constraint check. Defaults to a subroutine that always returns true. Yeah, she's easygoing like that. ✨
  • message β€” a subroutine reference used to generate the error message when the constraint fails. Optional!
  • coercion β€” a Moose::Meta::TypeCoercion object representing the coercions into this type. Also optional.
  • inlined β€” a subroutine that returns a string suitable for inlining this type constraint. It gets called as a method on the type constraint object, and it receives one extra parameter: a variable name to be tested (usually $_ or $_[0].

Optional, bestie. 🫢

  • inline_environment β€” a hash reference of variables to close over. The keys are variable names and the values are references to those variables.

⭐ $constraint->equals($type_name_or_object)

Returns true if the name or type object you passed in is the same as the current type. Twin check: passed. πŸ‘―

πŸ’œ $constraint->is_subtype_of($type_name_or_object)

Returns true if the name or type object you passed in is a parent of the current type.

🌈 $constraint->is_a_type_of($type_name_or_object)

Returns true if the given type is the same as the current type, or is a parent of the current type. It's basically a shortcut for checking equals and is_subtype_of β€” two birds, one slay. πŸ•ŠοΈ

πŸ’– $constraint->coerce($value)

Attempts to coerce the value into the type. Heads up though: if the type doesn't have any defined coercions, this throws an error. Not a vibe, but it is what it is. 😭

If no coercion can produce a value matching $constraint, you just get the original value back. She tried her best.

πŸŽ€ $constraint->assert_coerce($value)

Behaves just like coerce, but with consequences: if the result isn't valid according to $constraint, an error gets thrown. Periodt.

πŸ¦‹ $constraint->check($value)

Returns true if the given value passes the constraint for the type. Simple, clean, main character energy.

🧚 $constraint->validate($value)

Similar to check, but with a lil plot twist: if the type is valid, the method returns an explicit undef. If the type is not valid, we call $self->get_message($value) internally to generate an error message.

πŸ‘‘ $constraint->assert_valid($value)

Like check and validate, this checks whether $value is valid under the constraint. If it is, she returns true. If she's not valid, an exception gets thrown with the results of $self->get_message($value). No silent failures in this house. πŸ’…

🌸 $constraint->name

Returns the type's name, exactly as provided to the constructor.

πŸ’œ $constraint->parent

Returns the type's parent, exactly as provided to the constructor β€” if there is one, anyway.

✨ $constraint->has_parent

Returns true if the type has a parent type. Yes or no, no in-between.

πŸ“ $constraint->parents

Returns all of the type's parents as a list of type constraint objects. Whole family reunion. 🫢

πŸ”₯ $constraint->constraint

Returns the type's constraint, as provided to the constructor.

πŸ’– $constraint->get_message($value)

Generates a message for the given value. If the type doesn't have an explicit message of her own, we generate a default one. She always has something to say.

🌈 $constraint->has_message

Returns true if the type has a message.

⭐ $constraint->message

Returns the type's message as a subroutine reference.

πŸŽ€ $constraint->coercion

Returns the type's Moose::Meta::TypeCoercion object, if one exists.

🧚 $constraint->has_coercion

Returns true if the type has a coercion.

πŸ¦‹ $constraint->can_be_inlined

Returns true if this type constraint can be inlined. Real talk: a type constraint which subtypes an inlinable constraint and does not add an additional constraint basically "inherits" its parent type's inlining. Generational talent. πŸ‘‘

πŸ’œ $constraint->create_child_type(%options)

Returns a new type constraint of the same class, built from the %options you give her. The parent option will be the current type β€” so congrats, you're a parent now. πŸ₯Ή

This method exists so that subclasses of this class can override the behavior and change how child types get created. Customizable, as she should be.


πŸ’€ BUGS πŸ’€

See "BUGS" in Moose for the details on reporting bugs. Go tell them, bestie, they wanna know. 🫢


🌟 AUTHORS 🌟

  • 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>

Look at that roster. The Avengers could never. πŸ’…βœ¨


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. Free as in freedom, slay. πŸ•ŠοΈ


okay that's the whole page, ily, go write some type constraints πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«ΆπŸ‘‘πŸŒˆβ­

signing off from perl v5.42.0, dated 2025-07-25, in the Moose::...nstraint(3) section β€” byeeee πŸ‘‹πŸ’œ

πŸ“¦ where this page comes from