Your IP : 216.73.216.74


Current Path : /usr/local/share/man/man3/
Upload File :
Current File : //usr/local/share/man/man3/Moose::Util::TypeConstraints.3pm

.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Moose::Util::TypeConstraints 3"
.TH Moose::Util::TypeConstraints 3 "2021-11-07" "perl v5.26.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Moose::Util::TypeConstraints \- Type constraint system for Moose
.SH "VERSION"
.IX Header "VERSION"
version 2.2201
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&  use Moose::Util::TypeConstraints;
\&
\&  subtype \*(AqNatural\*(Aq,
\&      as \*(AqInt\*(Aq,
\&      where { $_ > 0 };
\&
\&  subtype \*(AqNaturalLessThanTen\*(Aq,
\&      as \*(AqNatural\*(Aq,
\&      where { $_ < 10 },
\&      message { "This number ($_) is not less than ten!" };
\&
\&  coerce \*(AqNum\*(Aq,
\&      from \*(AqStr\*(Aq,
\&      via { 0+$_ };
\&
\&  class_type \*(AqDateTimeClass\*(Aq, { class => \*(AqDateTime\*(Aq };
\&
\&  role_type \*(AqBarks\*(Aq, { role => \*(AqSome::Library::Role::Barks\*(Aq };
\&
\&  enum \*(AqRGBColors\*(Aq, [qw(red green blue)];
\&
\&  union \*(AqStringOrArray\*(Aq, [qw( String ArrayRef )];
\&
\&  no Moose::Util::TypeConstraints;
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module provides Moose with the ability to create custom type
constraints to be used in attribute definition.
.SS "Important Caveat"
.IX Subsection "Important Caveat"
This is \fB\s-1NOT\s0\fR a type system for Perl 5. These are type constraints,
and they are not used by Moose unless you tell it to. No type
inference is performed, expressions are not typed, etc. etc. etc.
.PP
A type constraint is at heart a small \*(L"check if a value is valid\*(R"
function. A constraint can be associated with an attribute. This
simplifies parameter validation, and makes your code clearer to read,
because you can refer to constraints by name.
.SS "Slightly Less Important Caveat"
.IX Subsection "Slightly Less Important Caveat"
It is \fBalways\fR a good idea to quote your type names.
.PP
This prevents Perl from trying to execute the call as an indirect
object call. This can be an issue when you have a subtype with the
same name as a valid class.
.PP
For instance:
.PP
.Vb 1
\&  subtype DateTime => as Object => where { $_\->isa(\*(AqDateTime\*(Aq) };
.Ve
.PP
will \fIjust work\fR, while this:
.PP
.Vb 2
\&  use DateTime;
\&  subtype DateTime => as Object => where { $_\->isa(\*(AqDateTime\*(Aq) };
.Ve
.PP
will fail silently and cause many headaches. The simple way to solve
this, as well as future proof your subtypes from classes which have
yet to have been created, is to quote the type name:
.PP
.Vb 2
\&  use DateTime;
\&  subtype \*(AqDateTime\*(Aq, as \*(AqObject\*(Aq, where { $_\->isa(\*(AqDateTime\*(Aq) };
.Ve
.SS "Default Type Constraints"
.IX Subsection "Default Type Constraints"
This module also provides a simple hierarchy for Perl 5 types, here is
that hierarchy represented visually.
.PP
.Vb 10
\&  Any
\&      Item
\&          Bool
\&          Maybe[\`a]
\&          Undef
\&          Defined
\&              Value
\&                  Str
\&                      Num
\&                          Int
\&                      ClassName
\&                      RoleName
\&              Ref
\&                  ScalarRef[\`a]
\&                  ArrayRef[\`a]
\&                  HashRef[\`a]
\&                  CodeRef
\&                  RegexpRef
\&                  GlobRef
\&                  FileHandle
\&                  Object
.Ve
.PP
\&\fB\s-1NOTE:\s0\fR Any type followed by a type parameter \f(CW\*(C`[\`a]\*(C'\fR can be
parameterized, this means you can say:
.PP
.Vb 4
\&  ArrayRef[Int]    # an array of integers
\&  HashRef[CodeRef] # a hash of str to CODE ref mappings
\&  ScalarRef[Int]   # a reference to an integer
\&  Maybe[Str]       # value may be a string, may be undefined
.Ve
.PP
If Moose finds a name in brackets that it does not recognize as an
existing type, it assumes that this is a class name, for example
\&\f(CW\*(C`ArrayRef[DateTime]\*(C'\fR.
.PP
\&\fB\s-1NOTE:\s0\fR Unless you parameterize a type, then it is invalid to include
the square brackets. I.e. \f(CW\*(C`ArrayRef[]\*(C'\fR will be treated as a new type
name, \fInot\fR as a parameterization of \f(CW\*(C`ArrayRef\*(C'\fR.
.PP
\&\fB\s-1NOTE:\s0\fR The \f(CW\*(C`Undef\*(C'\fR type constraint for the most part works
correctly now, but edge cases may still exist, please use it
sparingly.
.PP
\&\fB\s-1NOTE:\s0\fR The \f(CW\*(C`ClassName\*(C'\fR type constraint does a complex package
existence check. This means that your class \fBmust\fR be loaded for this
type constraint to pass.
.PP
\&\fB\s-1NOTE:\s0\fR The \f(CW\*(C`RoleName\*(C'\fR constraint checks a string is a \fIpackage
name\fR which is a role, like \f(CW\*(AqMyApp::Role::Comparable\*(Aq\fR.
.SS "Type Constraint Naming"
.IX Subsection "Type Constraint Naming"
Type names declared via this module can only contain alphanumeric
characters, colons (:), and periods (.).
.PP
Since the types created by this module are global, it is suggested
that you namespace your types just as you would namespace your
modules. So instead of creating a \fIColor\fR type for your
\&\fBMy::Graphics\fR module, you would call the type
\&\fIMy::Graphics::Types::Color\fR instead.
.SS "Use with Other Constraint Modules"
.IX Subsection "Use with Other Constraint Modules"
This module can play nicely with other constraint modules with some
slight tweaking. The \f(CW\*(C`where\*(C'\fR clause in types is expected to be a
\&\f(CW\*(C`CODE\*(C'\fR reference which checks its first argument and returns a
boolean. Since most constraint modules work in a similar way, it
should be simple to adapt them to work with Moose.
.PP
For instance, this is how you could use it with
Declare::Constraints::Simple to declare a completely new type.
.PP
.Vb 7
\&  type \*(AqHashOfArrayOfObjects\*(Aq,
\&      where {
\&          IsHashRef(
\&              \-keys   => HasLength,
\&              \-values => IsArrayRef(IsObject)
\&          )\->(@_);
\&      };
.Ve
.PP
For more examples see the \fIt/examples/example_w_DCS.t\fR test
file.
.PP
Here is an example of using Test::Deep and its non-test
related \f(CW\*(C`eq_deeply\*(C'\fR function.
.PP
.Vb 8
\&  type \*(AqArrayOfHashOfBarsAndRandomNumbers\*(Aq,
\&      where {
\&          eq_deeply($_,
\&              array_each(subhashof({
\&                  bar           => isa(\*(AqBar\*(Aq),
\&                  random_number => ignore()
\&              })))
\&        };
.Ve
.PP
For a complete example see the
\&\fIt/examples/example_w_TestDeep.t\fR test file.
.SS "Error messages"
.IX Subsection "Error messages"
Type constraints can also specify custom error messages, for when they fail to
validate. This is provided as just another coderef, which receives the invalid
value in \f(CW$_\fR, as in:
.PP
.Vb 4
\&  subtype \*(AqPositiveInt\*(Aq,
\&       as \*(AqInt\*(Aq,
\&       where { $_ > 0 },
\&       message { "$_ is not a positive integer!" };
.Ve
.PP
If no message is specified, a default message will be used, which indicates
which type constraint was being used and what value failed. If
Devel::PartialDump (version 0.14 or higher) is installed, it will be used to
display the invalid value, otherwise it will just be printed as is.
.SH "FUNCTIONS"
.IX Header "FUNCTIONS"
.SS "Type Constraint Constructors"
.IX Subsection "Type Constraint Constructors"
The following functions are used to create type constraints.  They
will also register the type constraints your create in a global
registry that is used to look types up by name.
.PP
See the \*(L"\s-1SYNOPSIS\*(R"\s0 for an example of how to use these.
.PP
\fIsubtype 'Name', as 'Parent', where { } ...\fR
.IX Subsection "subtype 'Name', as 'Parent', where { } ..."
.PP
This creates a named subtype.
.PP
If you provide a parent that Moose does not recognize, it will
automatically create a new class type constraint for this name.
.PP
When creating a named type, the \f(CW\*(C`subtype\*(C'\fR function should either be
called with the sugar helpers (\f(CW\*(C`where\*(C'\fR, \f(CW\*(C`message\*(C'\fR, etc), or with a
name and a hashref of parameters:
.PP
.Vb 1
\& subtype( \*(AqFoo\*(Aq, { where => ..., message => ... } );
.Ve
.PP
The valid hashref keys are \f(CW\*(C`as\*(C'\fR (the parent), \f(CW\*(C`where\*(C'\fR, \f(CW\*(C`message\*(C'\fR,
and \f(CW\*(C`inline_as\*(C'\fR.
.PP
\fIsubtype as 'Parent', where { } ...\fR
.IX Subsection "subtype as 'Parent', where { } ..."
.PP
This creates an unnamed subtype and will return the type
constraint meta-object, which will be an instance of
Moose::Meta::TypeConstraint.
.PP
When creating an anonymous type, the \f(CW\*(C`subtype\*(C'\fR function should either
be called with the sugar helpers (\f(CW\*(C`where\*(C'\fR, \f(CW\*(C`message\*(C'\fR, etc), or with
just a hashref of parameters:
.PP
.Vb 1
\& subtype( { where => ..., message => ... } );
.Ve
.PP
\fIclass_type ($class, ?$options)\fR
.IX Subsection "class_type ($class, ?$options)"
.PP
Creates a new subtype of \f(CW\*(C`Object\*(C'\fR with the name \f(CW$class\fR and the
metaclass Moose::Meta::TypeConstraint::Class.
.PP
.Vb 2
\&  # Create a type called \*(AqBox\*(Aq which tests for objects which \->isa(\*(AqBox\*(Aq)
\&  class_type \*(AqBox\*(Aq;
.Ve
.PP
By default, the name of the type and the name of the class are the same, but
you can specify both separately.
.PP
.Vb 2
\&  # Create a type called \*(AqBox\*(Aq which tests for objects which \->isa(\*(AqObjectLibrary::Box\*(Aq);
\&  class_type \*(AqBox\*(Aq, { class => \*(AqObjectLibrary::Box\*(Aq };
.Ve
.PP
\fIrole_type ($role, ?$options)\fR
.IX Subsection "role_type ($role, ?$options)"
.PP
Creates a \f(CW\*(C`Role\*(C'\fR type constraint with the name \f(CW$role\fR and the
metaclass Moose::Meta::TypeConstraint::Role.
.PP
.Vb 2
\&  # Create a type called \*(AqWalks\*(Aq which tests for objects which \->does(\*(AqWalks\*(Aq)
\&  role_type \*(AqWalks\*(Aq;
.Ve
.PP
By default, the name of the type and the name of the role are the same, but
you can specify both separately.
.PP
.Vb 2
\&  # Create a type called \*(AqWalks\*(Aq which tests for objects which \->does(\*(AqMooseX::Role::Walks\*(Aq);
\&  role_type \*(AqWalks\*(Aq, { role => \*(AqMooseX::Role::Walks\*(Aq };
.Ve
.PP
\fImaybe_type ($type)\fR
.IX Subsection "maybe_type ($type)"
.PP
Creates a type constraint for either \f(CW\*(C`undef\*(C'\fR or something of the
given type.
.PP
\fIduck_type ($name, \e@methods)\fR
.IX Subsection "duck_type ($name, @methods)"
.PP
This will create a subtype of Object and test to make sure the value
\&\f(CW\*(C`can()\*(C'\fR do the methods in \f(CW\*(C`\e@methods\*(C'\fR.
.PP
This is intended as an easy way to accept non-Moose objects that
provide a certain interface. If you're using Moose classes, we
recommend that you use a \f(CW\*(C`requires\*(C'\fR\-only Role instead.
.PP
\fIduck_type (\e@methods)\fR
.IX Subsection "duck_type (@methods)"
.PP
If passed an \s-1ARRAY\s0 reference as the only parameter instead of the
\&\f(CW$name\fR, \f(CW\*(C`\e@methods\*(C'\fR pair, this will create an unnamed duck type.
This can be used in an attribute definition like so:
.PP
.Vb 4
\&  has \*(Aqcache\*(Aq => (
\&      is  => \*(Aqro\*(Aq,
\&      isa => duck_type( [qw( get_set )] ),
\&  );
.Ve
.PP
\fIenum ($name, \e@values)\fR
.IX Subsection "enum ($name, @values)"
.PP
This will create a basic subtype for a given set of strings.
The resulting constraint will be a subtype of \f(CW\*(C`Str\*(C'\fR and
will match any of the items in \f(CW\*(C`\e@values\*(C'\fR. It is case sensitive.
See the \*(L"\s-1SYNOPSIS\*(R"\s0 for a simple example.
.PP
\&\fB\s-1NOTE:\s0\fR This is not a true proper enum type, it is simply
a convenient constraint builder.
.PP
\fIenum (\e@values)\fR
.IX Subsection "enum (@values)"
.PP
If passed an \s-1ARRAY\s0 reference as the only parameter instead of the
\&\f(CW$name\fR, \f(CW\*(C`\e@values\*(C'\fR pair, this will create an unnamed enum. This
can then be used in an attribute definition like so:
.PP
.Vb 4
\&  has \*(Aqsort_order\*(Aq => (
\&      is  => \*(Aqro\*(Aq,
\&      isa => enum([qw[ ascending descending ]]),
\&  );
.Ve
.PP
\fIunion ($name, \e@constraints)\fR
.IX Subsection "union ($name, @constraints)"
.PP
This will create a basic subtype where any of the provided constraints
may match in order to satisfy this constraint.
.PP
\fIunion (\e@constraints)\fR
.IX Subsection "union (@constraints)"
.PP
If passed an \s-1ARRAY\s0 reference as the only parameter instead of the
\&\f(CW$name\fR, \f(CW\*(C`\e@constraints\*(C'\fR pair, this will create an unnamed union.
This can then be used in an attribute definition like so:
.PP
.Vb 4
\&  has \*(Aqitems\*(Aq => (
\&      is => \*(Aqro\*(Aq,
\&      isa => union([qw[ Str ArrayRef ]]),
\&  );
.Ve
.PP
This is similar to the existing string union:
.PP
.Vb 1
\&  isa => \*(AqStr|ArrayRef\*(Aq
.Ve
.PP
except that it supports anonymous elements as child constraints:
.PP
.Vb 4
\&  has \*(Aqcolor\*(Aq => (
\&    isa => \*(Aqro\*(Aq,
\&    isa => union([ \*(AqInt\*(Aq,  enum([qw[ red green blue ]]) ]),
\&  );
.Ve
.PP
\fIas 'Parent'\fR
.IX Subsection "as 'Parent'"
.PP
This is just sugar for the type constraint construction syntax.
.PP
It takes a single argument, which is the name of a parent type.
.PP
\fIwhere { ... }\fR
.IX Subsection "where { ... }"
.PP
This is just sugar for the type constraint construction syntax.
.PP
It takes a subroutine reference as an argument. When the type
constraint is tested, the reference is run with the value to be tested
in \f(CW$_\fR. This reference should return true or false to indicate
whether or not the constraint check passed.
.PP
\fImessage { ... }\fR
.IX Subsection "message { ... }"
.PP
This is just sugar for the type constraint construction syntax.
.PP
It takes a subroutine reference as an argument. When the type
constraint fails, then the code block is run with the value provided
in \f(CW$_\fR. This reference should return a string, which will be used in
the text of the exception thrown.
.PP
\fIinline_as { ... }\fR
.IX Subsection "inline_as { ... }"
.PP
This can be used to define a \*(L"hand optimized\*(R" inlinable version of your type
constraint.
.PP
You provide a subroutine which will be called \fIas a method\fR on a
Moose::Meta::TypeConstraint object. It will receive a single parameter, the
name of the variable to check, typically something like \f(CW"$_"\fR or \f(CW"$_[0]"\fR.
.PP
The subroutine should return a code string suitable for inlining. You can
assume that the check will be wrapped in parentheses when it is inlined.
.PP
The inlined code should include any checks that your type's parent types
do. If your parent type constraint defines its own inlining, you can simply use
that to avoid repeating code. For example, here is the inlining code for the
\&\f(CW\*(C`Value\*(C'\fR type, which is a subtype of \f(CW\*(C`Defined\*(C'\fR:
.PP
.Vb 4
\&    sub {
\&        $_[0]\->parent()\->_inline_check($_[1])
\&        . \*(Aq && !ref(\*(Aq . $_[1] . \*(Aq)\*(Aq
\&    }
.Ve
.PP
\fItype 'Name', where { } ...\fR
.IX Subsection "type 'Name', where { } ..."
.PP
This creates a base type, which has no parent.
.PP
The \f(CW\*(C`type\*(C'\fR function should either be called with the sugar helpers
(\f(CW\*(C`where\*(C'\fR, \f(CW\*(C`message\*(C'\fR, etc), or with a name and a hashref of
parameters:
.PP
.Vb 1
\&  type( \*(AqFoo\*(Aq, { where => ..., message => ... } );
.Ve
.PP
The valid hashref keys are \f(CW\*(C`where\*(C'\fR, \f(CW\*(C`message\*(C'\fR, and \f(CW\*(C`inlined_as\*(C'\fR.
.SS "Type Constraint Utilities"
.IX Subsection "Type Constraint Utilities"
\fImatch_on_type \f(CI$value\fI => ( \f(CI$type\fI => \e&action, ... ?\e&default )\fR
.IX Subsection "match_on_type $value => ( $type => &action, ... ?&default )"
.PP
This is a utility function for doing simple type based dispatching similar to
match/case in OCaml and case/of in Haskell. It is not as featureful as those
languages, nor does not it support any kind of automatic destructuring
bind. Here is a simple Perl pretty printer dispatching over the core Moose
types.
.PP
.Vb 10
\&  sub ppprint {
\&      my $x = shift;
\&      match_on_type $x => (
\&          HashRef => sub {
\&              my $hash = shift;
\&              \*(Aq{ \*(Aq
\&                  . (
\&                  join ", " => map { $_ . \*(Aq => \*(Aq . ppprint( $hash\->{$_} ) }
\&                      sort keys %$hash
\&                  ) . \*(Aq }\*(Aq;
\&          },
\&          ArrayRef => sub {
\&              my $array = shift;
\&              \*(Aq[ \*(Aq . ( join ", " => map { ppprint($_) } @$array ) . \*(Aq ]\*(Aq;
\&          },
\&          CodeRef   => sub {\*(Aqsub { ... }\*(Aq},
\&          RegexpRef => sub { \*(Aqqr/\*(Aq . $_ . \*(Aq/\*(Aq },
\&          GlobRef   => sub { \*(Aq*\*(Aq . B::svref_2object($_)\->NAME },
\&          Object    => sub { $_\->can(\*(Aqto_string\*(Aq) ? $_\->to_string : $_ },
\&          ScalarRef => sub { \*(Aq\e\e\*(Aq . ppprint( ${$_} ) },
\&          Num       => sub {$_},
\&          Str       => sub { \*(Aq"\*(Aq . $_ . \*(Aq"\*(Aq },
\&          Undef     => sub {\*(Aqundef\*(Aq},
\&          => sub { die "I don\*(Aqt know what $_ is" }
\&      );
\&  }
.Ve
.PP
Or a simple \s-1JSON\s0 serializer:
.PP
.Vb 10
\&  sub to_json {
\&      my $x = shift;
\&      match_on_type $x => (
\&          HashRef => sub {
\&              my $hash = shift;
\&              \*(Aq{ \*(Aq
\&                  . (
\&                  join ", " =>
\&                      map { \*(Aq"\*(Aq . $_ . \*(Aq" : \*(Aq . to_json( $hash\->{$_} ) }
\&                      sort keys %$hash
\&                  ) . \*(Aq }\*(Aq;
\&          },
\&          ArrayRef => sub {
\&              my $array = shift;
\&              \*(Aq[ \*(Aq . ( join ", " => map { to_json($_) } @$array ) . \*(Aq ]\*(Aq;
\&          },
\&          Num   => sub {$_},
\&          Str   => sub { \*(Aq"\*(Aq . $_ . \*(Aq"\*(Aq },
\&          Undef => sub {\*(Aqnull\*(Aq},
\&          => sub { die "$_ is not acceptable json type" }
\&      );
\&  }
.Ve
.PP
The matcher is done by mapping a \f(CW$type\fR to an \f(CW\*(C`\e&action\*(C'\fR. The \f(CW$type\fR can
be either a string type or a Moose::Meta::TypeConstraint object, and
\&\f(CW\*(C`\e&action\*(C'\fR is a subroutine reference. This function will dispatch on the
first match for \f(CW$value\fR. It is possible to have a catch-all by providing an
additional subroutine reference as the final argument to \f(CW\*(C`match_on_type\*(C'\fR.
.SS "Type Coercion Constructors"
.IX Subsection "Type Coercion Constructors"
You can define coercions for type constraints, which allow you to
automatically transform values to something valid for the type
constraint. If you ask your accessor to coerce by adding the option \f(CW\*(C`coerce => 1\*(C'\fR, then Moose will run
the type-coercion code first, followed by the type constraint
check. This feature should be used carefully as it is very powerful
and could easily take off a limb if you are not careful.
.PP
See the \*(L"\s-1SYNOPSIS\*(R"\s0 for an example of how to use these.
.PP
\fIcoerce 'Name', from 'OtherName', via { ... }\fR
.IX Subsection "coerce 'Name', from 'OtherName', via { ... }"
.PP
This defines a coercion from one type to another. The \f(CW\*(C`Name\*(C'\fR argument
is the type you are coercing \fIto\fR.
.PP
To define multiple coercions, supply more sets of from/via pairs:
.PP
.Vb 3
\&  coerce \*(AqName\*(Aq,
\&    from \*(AqOtherName\*(Aq, via { ... },
\&    from \*(AqThirdName\*(Aq, via { ... };
.Ve
.PP
\fIfrom 'OtherName'\fR
.IX Subsection "from 'OtherName'"
.PP
This is just sugar for the type coercion construction syntax.
.PP
It takes a single type name (or type object), which is the type being
coerced \fIfrom\fR.
.PP
\fIvia { ... }\fR
.IX Subsection "via { ... }"
.PP
This is just sugar for the type coercion construction syntax.
.PP
It takes a subroutine reference. This reference will be called with
the value to be coerced in \f(CW$_\fR. It is expected to return a new value
of the proper type for the coercion.
.SS "Creating and Finding Type Constraints"
.IX Subsection "Creating and Finding Type Constraints"
These are additional functions for creating and finding type
constraints. Most of these functions are not available for
importing. The ones that are importable as specified.
.PP
\fIfind_type_constraint($type_name)\fR
.IX Subsection "find_type_constraint($type_name)"
.PP
This function can be used to locate the Moose::Meta::TypeConstraint
object for a named type.
.PP
This function is importable.
.PP
\fIregister_type_constraint($type_object)\fR
.IX Subsection "register_type_constraint($type_object)"
.PP
This function will register a Moose::Meta::TypeConstraint with the
global type registry.
.PP
This function is importable.
.PP
\fInormalize_type_constraint_name($type_constraint_name)\fR
.IX Subsection "normalize_type_constraint_name($type_constraint_name)"
.PP
This method takes a type constraint name and returns the normalized
form. This removes any whitespace in the string.
.PP
\fIcreate_type_constraint_union($pipe_separated_types | \f(CI@type_constraint_names\fI)\fR
.IX Subsection "create_type_constraint_union($pipe_separated_types | @type_constraint_names)"
.PP
\fIcreate_named_type_constraint_union($name, \f(CI$pipe_separated_types\fI | \f(CI@type_constraint_names\fI)\fR
.IX Subsection "create_named_type_constraint_union($name, $pipe_separated_types | @type_constraint_names)"
.PP
This can take a union type specification like \f(CW\*(AqInt|ArrayRef[Int]\*(Aq\fR,
or a list of names. It returns a new
Moose::Meta::TypeConstraint::Union object.
.PP
\fIcreate_parameterized_type_constraint($type_name)\fR
.IX Subsection "create_parameterized_type_constraint($type_name)"
.PP
Given a \f(CW$type_name\fR in the form of \f(CW\*(AqBaseType[ContainerType]\*(Aq\fR,
this will create a new Moose::Meta::TypeConstraint::Parameterized
object. The \f(CW\*(C`BaseType\*(C'\fR must already exist as a parameterizable
type.
.PP
\fIcreate_class_type_constraint($class, \f(CI$options\fI)\fR
.IX Subsection "create_class_type_constraint($class, $options)"
.PP
Given a class name this function will create a new
Moose::Meta::TypeConstraint::Class object for that class name.
.PP
The \f(CW$options\fR is a hash reference that will be passed to the
Moose::Meta::TypeConstraint::Class constructor (as a hash).
.PP
\fIcreate_role_type_constraint($role, \f(CI$options\fI)\fR
.IX Subsection "create_role_type_constraint($role, $options)"
.PP
Given a role name this function will create a new
Moose::Meta::TypeConstraint::Role object for that role name.
.PP
The \f(CW$options\fR is a hash reference that will be passed to the
Moose::Meta::TypeConstraint::Role constructor (as a hash).
.PP
\fIcreate_enum_type_constraint($name, \f(CI$values\fI)\fR
.IX Subsection "create_enum_type_constraint($name, $values)"
.PP
Given a enum name this function will create a new
Moose::Meta::TypeConstraint::Enum object for that enum name.
.PP
\fIcreate_duck_type_constraint($name, \f(CI$methods\fI)\fR
.IX Subsection "create_duck_type_constraint($name, $methods)"
.PP
Given a duck type name this function will create a new
Moose::Meta::TypeConstraint::DuckType object for that enum name.
.PP
\fIfind_or_parse_type_constraint($type_name)\fR
.IX Subsection "find_or_parse_type_constraint($type_name)"
.PP
Given a type name, this first attempts to find a matching constraint
in the global registry.
.PP
If the type name is a union or parameterized type, it will create a
new object of the appropriate, but if given a \*(L"regular\*(R" type that does
not yet exist, it simply returns false.
.PP
When given a union or parameterized type, the member or base type must
already exist.
.PP
If it creates a new union or parameterized type, it will add it to the
global registry.
.PP
\fIfind_or_create_isa_type_constraint($type_name)\fR
.IX Subsection "find_or_create_isa_type_constraint($type_name)"
.PP
\fIfind_or_create_does_type_constraint($type_name)\fR
.IX Subsection "find_or_create_does_type_constraint($type_name)"
.PP
These functions will first call \f(CW\*(C`find_or_parse_type_constraint\*(C'\fR. If
that function does not return a type, a new type object will
be created.
.PP
The \f(CW\*(C`isa\*(C'\fR variant will use \f(CW\*(C`create_class_type_constraint\*(C'\fR and the
\&\f(CW\*(C`does\*(C'\fR variant will use \f(CW\*(C`create_role_type_constraint\*(C'\fR.
.PP
\fIget_type_constraint_registry\fR
.IX Subsection "get_type_constraint_registry"
.PP
Returns the Moose::Meta::TypeConstraint::Registry object which
keeps track of all type constraints.
.PP
\fIlist_all_type_constraints\fR
.IX Subsection "list_all_type_constraints"
.PP
This will return a list of type constraint names in the global
registry. You can then fetch the actual type object using
\&\f(CW\*(C`find_type_constraint($type_name)\*(C'\fR.
.PP
\fIlist_all_builtin_type_constraints\fR
.IX Subsection "list_all_builtin_type_constraints"
.PP
This will return a list of builtin type constraints, meaning those
which are defined in this module. See the \*(L"Default Type Constraints\*(R"
section for a complete list.
.PP
\fIexport_type_constraints_as_functions\fR
.IX Subsection "export_type_constraints_as_functions"
.PP
This will export all the current type constraints as functions into
the caller's namespace (\f(CW\*(C`Int()\*(C'\fR, \f(CW\*(C`Str()\*(C'\fR, etc). Right now, this is
mostly used for testing, but it might prove useful to others.
.PP
\fIget_all_parameterizable_types\fR
.IX Subsection "get_all_parameterizable_types"
.PP
This returns all the parameterizable types that have been registered,
as a list of type objects.
.PP
\fIadd_parameterizable_type($type)\fR
.IX Subsection "add_parameterizable_type($type)"
.PP
Adds \f(CW$type\fR to the list of parameterizable types
.SH "BUGS"
.IX Header "BUGS"
See \*(L"\s-1BUGS\*(R"\s0 in Moose for details on reporting bugs.
.SH "AUTHORS"
.IX Header "AUTHORS"
.IP "\(bu" 4
Stevan Little <stevan@cpan.org>
.IP "\(bu" 4
Dave Rolsky <autarch@urth.org>
.IP "\(bu" 4
Jesse Luehrs <doy@cpan.org>
.IP "\(bu" 4
Shawn M Moore <sartak@cpan.org>
.IP "\(bu" 4
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
.IP "\(bu" 4
Karen Etheridge <ether@cpan.org>
.IP "\(bu" 4
Florian Ragwitz <rafl@debian.org>
.IP "\(bu" 4
Hans Dieter Pearcey <hdp@cpan.org>
.IP "\(bu" 4
Chris Prather <chris@prather.org>
.IP "\(bu" 4
Matt S Trout <mstrout@cpan.org>
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
This software is copyright (c) 2006 by Infinity Interactive, Inc.
.PP
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.