Type::Coercion(3)
π NAME π
Type::Coercion β bestie, it's literally a whole set of coercions aimed at one specific target type constraint. That's the whole vibe. It's Type::Coercion(3), third section of the manual, and she's here to convert your values for you. π
β¨ STATUS β¨
This module is covered by the Type-Tiny stability policy. So likeβ¦ she's stable, she's not going anywhere, we love that for her. π«Ά
πΈ DESCRIPTION πΈ
π Constructors
new(%attributes)β the classic Moose-style constructor function, doing exactly what you'd expect a constructor to do. β¨add($c1, $c2)β okay so this one takes two existingType::Coercionobjects and makes a brand-newType::Coercionout of them. It's giving "collab".
π¦ Attributes
Attributes are named values you can pass to the constructor, and every single one of them has a matching reader method. For example:
my $c = Type::Coercion->new( type_constraint => Int );
my $t = $c->type_constraint; # Int
π Important attributes
These are the ones you're most likely to actually want to pass in when you're building your own type coercions, and the ones you'll be most interested in reading back out when you're handling coercion objects. Main character attributes, no cap.
type_constraintβ a weak reference to the target type constraint, aka the type constraint that the output of your coercion coderefs is expected to conform to. She's the goal. πtype_coercion_mapβ an arrayref of source-type/code pairs. Yeah. A map. Of pairs. πΊοΈfrozenβ boolean, and the default is false. If a coercion is frozen, you cannot calladd_type_coercionson it. She's locked in, literally. πnameβ a name for the coercion. It has to follow certain naming rules: it must begin with an uppercase letter, and then keep going using only letters, digits0-9and underscores. It's optional; if you don't supply one, you get an anonymous coercion. Mysterious. β¨display_nameβ the name shown for the coercion when it gets stringified. These ones don't have to follow any naming rules at all, go feral bestie. Optional; a default name will be calculated from thename.libraryβ the package name of the type library this coercion is associated with. Optional. It's informational only, so heads up: setting this attribute does not install the coercion into the package. It's just a label, not a spell. π§
π Attributes related to parameterizable and parameterized coercions
These next ones get used for parameterized coercions, but they're not fully documented because they might change in the near future. So like, she's a work in progress, don't get too attached. π
coercion_generatorparametersparameterized_from
β Lazy generated attributes
These ones you should not usually pass to the constructor. Unless you're doing something especially unusual, just let the default lazily-built return values do their thing. She'll handle it. π
compiled_coercionβ a coderef to coerce a value ($_[0]).
The whole point of this attribute is that you should not set it β rely on the lazily-built default instead. Type::Coerce will usually generate a pretty fast coderef, inlining all the type constraint checks and everything. She's optimized. π₯
moose_coercionβ aMoose::Meta::TypeCoercionobject equivalent to this one. Do not set this manually, okay? Rely on the default built one. π«Ά
π Methods
π Predicate methods
These return booleans telling you stuff about the coercion, and each one is tightly associated with a particular attribute. (Go look at "Attributes" if you need a refresher.)
has_type_constraint,has_libraryβ simple Moose-style predicate methods that indicate the presence or absence of an attribute. Present or not present, that's it. βis_anonβ returns true iff the coercion does not have aname. Anonymous queen. π
The following predicates are used for parameterized coercions, but are not fully documented because they may change in the near future:
has_coercion_generatorhas_parametersis_parameterizableis_parameterized
π« Coercion
These are the methods that actually coerce values to a type constraint. The moment you've been waiting for.
coerce($value)β coerce the value to the target type.
Returns the coerced value, or the original value if no coercion was possible. She tries her best, and if she can't, she hands it back unchanged. π
check_coerce($value)β coerces the value to the target type, and checks that the coerced value passes the target type constraint.
Returns the coerced value if it passes the target type constraint, or undef if it doesn't. Vibe check passed? Slay. Vibe check failed? undef.
assert_coerce($value)β coerces the value to the target type, and throws an exception if the result does not validate against the target type constraint.
Returns the coerced value.
But here's the tea: if this type coercion does not have an associated target type constraint, then it will not attempt to validate against it, and will always return the coerced value, never throwing an exception (in theory). In theory. π
π§ Coercion code definition methods
Every single one of these returns $self, so they're all suitable for chaining. Chain them, bestie. It's giving fluent interface. β¨
add_type_coercions($type1, $code1, ...)β takes one or more pairs of Type::Tiny constraints and coercion code, creating an ordered list of source types and coercion codes.
Coercion codes can be expressed as either a string of Perl code (this includes objects which overload stringification), or a coderef (or an object that overloads coderefification). In either case, the value to be coerced is $_.
add_type_coercions($coercion_object) also works, and you can use it to copy coercions from another type constraint:
$type->coercion->add_type_coercions($othertype->coercion)->freeze;
freezeβ sets thefrozenattribute to true.Type::Tinycalls this automatically sometimes, so like, don't be surprised. π₯Άi_really_want_to_unfreezeβ if you really want to unfreeze a coercion, call this method.
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). You have been warned, bestie. π
π Parameterization
The following method is used for parameterized coercions, but is not fully documented because it may change in the near future:
parameterize(@params)
π₯ Type coercion introspection methods
These let you figure out how a coercion relates to type constraints. Investigative journalism, but for types.
has_coercion_for_type($source_type)β returns true iff this coercion has a coercion from the source type.
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.) Yes, "0 but true" β she's a real Perl string, she's iconic, she's confusing, she's everything. π
has_coercion_for_value($value)β returns true iff the value could be coerced by this coercion.
Returns the special string "0 but true" if no coercion would actually be necessary for this value, because it already meets the target type constraint. She's already perfect, no coercion needed. β¨
The type_constraint attribute provides a type constraint object for the target type constraint of the coercion. See "Attributes".
π Inlining methods
These generate strings of Perl code which you can paste into stringy evaluated subs to perform type coercions. Stringy eval, yes, we're doing that, don't look at me.
can_be_inlinedβ returns true iff the coercion can be inlined. π«Άinline_coercion($varname)β much likeinline_coercefrom Type::Tiny. Same energy, different name.
π Other methods
qualified_nameβ for non-anonymous coercions that have a library, returns a qualifiedMyLib::MyCoercionsort of name. Otherwise, returns the same asname. Simple as that.isa($class),can($method),AUTOLOAD(@args)β if Moose is loaded, then the combination of these methods is used to mock aMoose::Meta::TypeCoercion. Fake it till you make it. π
The following methods exist for Moose/Mouse compatibility, but do not do anything useful:
compile_type_coercionmeta
π¦ Overloading
- Boolification is overloaded to always return true. She's always true. Always. π
- Coderefification is overloaded to call
coerce. - On Perl 5.10.1 and above, smart match is overloaded to call
has_coercion_for_value.
Previous versions of Type::Coercion would overload the "+" operator to call add. Support for this was dropped after 0.040. Pour one out. π
π DIAGNOSTICS π
Attempt to add coercion code to a Type::Coercion which has been frozen
Type::Tiny type constraints are designed as immutable objects. Once you've created a constraint, rather than modifying it you generally create child constraints to do what you need.
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:
- Define as many of your coercions as possible within type libraries, not within the code that uses the type libraries. The type library will be evaluated relatively early, likely before there is any reason to freeze a coercion.
- If you do need to add coercions to a type within application code outside the type library, instead create a subtype and add coercions to that. The
plus_coercionsmethod provided by Type::Tiny should make this simple. π«Ά
π BUGS π
Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>. She wants to hear from you, bestie. π
β SEE ALSO β
Type::Tiny::Manual.Type::Tiny,Type::Library,Type::Utils,Types::Standard.Type::Coercion::Union.Moose::Meta::TypeCoercion.
π AUTHOR π
Toby Inkster <tobyink@cpan.org>. The icon. The legend. π«Ά
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2013-2014, 2017-2025 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. Free software, slay. β¨
πΈ DISCLAIMER OF WARRANTIES πΈ
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. No warranty, no promises, no cap. π
And that's the whole page, bestie β this was perl v5.42.0, dated 2026-01-17, for Type::Coercion(3). Go forth and coerce responsibly. πβ¨π¦
π¦ where this page comes from
- fedora 44: perl-Type-Tiny 2.010001-2.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms