Type::Coercion(3) | User Contributed Perl Documentation | Type::Coercion(3) |
Type::Coercion - a set of coercions to a particular target type constraint
This module is covered by the Type-Tiny stability policy.
Attributes are named values that may be passed to the constructor. For each attribute, there is a corresponding reader method. For example:
my $c = Type::Coercion->new( type_constraint => Int ); my $t = $c->type_constraint; # Int
Important attributes
These are the attributes you are likely to be most interested in providing when creating your own type coercions, and most interested in reading when dealing with coercion objects.
Optional; if not supplied will be an anonymous coercion.
Attributes related to parameterizable and parameterized coercions
The following attributes are used for parameterized coercions, but are not fully documented because they may change in the near future:
Lazy generated attributes
The following attributes should not be usually passed to the constructor; unless you're doing something especially unusual, you should rely on the default lazily-built return values.
The general point of this attribute is that you should not set it, but rely on the lazily-built default. Type::Coerce will usually generate a pretty fast coderef, inlining all type constraint checks, etc.
Predicate methods
These methods return booleans indicating information about the coercion. They are each tightly associated with a particular attribute. (See "Attributes".)
The following predicates are used for parameterized coercions, but are not fully documented because they may change in the near future:
Coercion
The following methods are used for coercing values to a type constraint:
Returns the coerced value, or the original value if no coercion was possible.
Returns the coerced value.
Coercion code definition methods
These methods all return $self so are suitable for chaining.
Coercion codes can be expressed as either a string of Perl code (this includes objects which overload stringification), or a coderef (or object that overloads coderefification). In either case, the value to be coerced is $_.
"add_type_coercions($coercion_object)" also works, and can be used to copy coercions from another type constraint:
$type->coercion->add_type_coercions($othertype->coercion)->freeze;
Don't call this method. It will potentially lead to subtle bugs.
This method is considered unstable; future versions of Type::Tiny may alter its behaviour (e.g. to throw an exception if it has been detected that unfreezing this particular coercion will cause bugs).
Parameterization
The following method is used for parameterized coercions, but is not fully documented because it may change in the near future:
Type coercion introspection methods
These methods allow you to determine a coercion's relationship to type constraints:
Returns the special string "0 but true" if no coercion should actually be necessary for this type. (For example, if a coercion coerces to a theoretical "Number" type, there is probably no coercion necessary for values that already conform to the "Integer" type.)
Returns the special string "0 but true" if no coercion would be actually be necessary for this value (due to it already meeting the target type constraint).
The "type_constraint" attribute provides a type constraint object for the target type constraint of the coercion. See "Attributes".
Inlining methods
The following methods are used to generate strings of Perl code which may be pasted into stringy "eval"uated subs to perform type coercions:
Other methods
The following methods exist for Moose/Mouse compatibility, but do not do anything useful.
Previous versions of Type::Coercion would overload the "+" operator to call "add". Support for this was dropped after 0.040.
Type::Coercion objects, on the other hand, are mutable. Coercion routines can be added at any time during the object's lifetime.
Sometimes Type::Tiny needs to freeze a Type::Coercion object to prevent this. In Moose and Mouse code this is likely to happen as soon as you use a type constraint in an attribute.
Workarounds:
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
Type::Tiny::Manual.
Type::Tiny, Type::Library, Type::Utils, Types::Standard.
Type::Coercion::Union.
Moose::Meta::TypeCoercion.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014, 2017-2020 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2020-10-28 | perl v5.34.0 |