Moose::Meta::TypeConstraint(3) User Contributed Perl Documentation Moose::Meta::TypeConstraint(3)

# [DESCRIPTION](#DESCRIPTION)

This class represents a single type constraint. Moose's built-in type constraints, as well as constraints you define, are all stored in a Moose::Meta::TypeConstraint::Registry object as objects of this class.

# [INHERITANCE](#INHERITANCE)

"Moose::Meta::TypeConstraint" is a subclass of Class::MOP::Object.

# [METHODS](#METHODS)

## [Moose::Meta::TypeConstraint-&gt;new(%options)](#Moose::Meta::TypeConstraint)

This creates a new type constraint based on the provided %options:

- name
  
  The constraint name. If a name is not provided, it will be set to "\_\_ANON\_\_".
- parent
  
  A "Moose::Meta::TypeConstraint" object which is the parent type for the type being created. This is optional.
- constraint
  
  This is the subroutine reference that implements the actual constraint check. This defaults to a subroutine which always returns true.
- message
  
  A subroutine reference which is used to generate an error message when the constraint fails. This is optional.
- coercion
  
  A Moose::Meta::TypeCoercion object representing the coercions to the type. This is optional.
- inlined
  
  A subroutine which returns a string suitable for inlining this type constraint. It will be called as a method on the type constraint object, and will receive a single additional parameter, a variable name to be tested (usually "$\_" or "$\_\[0]".
  
  This is optional.
- inline\_environment
  
  A hash reference of variables to close over. The keys are variables names, and the values are *references* to the variables.

## [$constraint-&gt;equals($type\_name\_or\_object)](#$constraint)

Returns true if the supplied name or type object is the same as the current type.

## [$constraint-&gt;is\_subtype\_of($type\_name\_or\_object)](#$constraint~2)

Returns true if the supplied name or type object is a parent of the current type.

## [$constraint-&gt;is\_a\_type\_of($type\_name\_or\_object)](#$constraint~3)

Returns true if the given type is the same as the current type, or is a parent of the current type. This is a shortcut for checking "equals" and "is\_subtype\_of".

## [$constraint-&gt;coerce($value)](#$constraint~4)

This will attempt to coerce the value to the type. If the type does not have any defined coercions this will throw an error.

If no coercion can produce a value matching $constraint, the original value is returned.

## [$constraint-&gt;assert\_coerce($value)](#$constraint~5)

This method behaves just like "coerce", but if the result is not valid according to $constraint, an error is thrown.

## [$constraint-&gt;check($value)](#$constraint~6)

Returns true if the given value passes the constraint for the type.

## [$constraint-&gt;validate($value)](#$constraint~7)

This is similar to "check". However, if the type *is valid* then the method returns an explicit "undef". If the type is not valid, we call "$self-&gt;get\_message($value)" internally to generate an error message.

## [$constraint-&gt;assert\_valid($value)](#$constraint~8)

Like "check" and "validate", this method checks whether $value is valid under the constraint. If it is, it will return true. If it is not, an exception will be thrown with the results of "$self-&gt;get\_message($value)".

## [$constraint-&gt;name](#$constraint~9)

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

## [$constraint-&gt;parent](#$constraint~10)

Returns the type's parent, as provided to the constructor, if any.

## [$constraint-&gt;has\_parent](#$constraint~11)

Returns true if the type has a parent type.

## [$constraint-&gt;parents](#$constraint~12)

Returns all of the types parents as an list of type constraint objects.

## [$constraint-&gt;constraint](#$constraint~13)

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

## [$constraint-&gt;get\_message($value)](#$constraint~14)

This generates a method for the given value. If the type does not have an explicit message, we generate a default message.

## [$constraint-&gt;has\_message](#$constraint~15)

Returns true if the type has a message.

## [$constraint-&gt;message](#$constraint~16)

Returns the type's message as a subroutine reference.

## [$constraint-&gt;coercion](#$constraint~17)

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

## [$constraint-&gt;has\_coercion](#$constraint~18)

Returns true if the type has a coercion.

## [$constraint-&gt;can\_be\_inlined](#$constraint~19)

Returns true if this type constraint can be inlined. A type constraint which subtypes an inlinable constraint and does not add an additional constraint "inherits" its parent type's inlining.

## [$constraint-&gt;create\_child\_type(%options)](#$constraint~20)

This returns a new type constraint of the same class using the provided %options. The "parent" option will be the current type.

This method exists so that subclasses of this class can override this behavior and change how child types are created.

# [BUGS](#BUGS)

See "BUGS" in Moose for details on reporting bugs.

2026-10-01 perl v5.44.0

---

Document: Moose::Meta::TypeConstraint(3pm) from perl-moose 2.4001-r0 (Chainguard OS)
Source: https://github.com/moose/Moose
