The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Type::Coercion - a set of coercions to a particular target type constraint

DESCRIPTION

Constructor

new(%attributes)

Moose-style constructor function.

Attributes

type_constraint

Weak reference to the target type constraint (i.e. the type constraint which the output of coercion coderefs is expected to conform to).

type_coercion_map

Arrayref of source-type/code pairs. Don't set this in the constructor; use the add_type_coercions method instead.

compiled_coercion

Coderef to coerce a value ($_[0]).

The general point of this attribute is that you should not set it, and rely on the lazily-built default. Type::Coerce will usually generate a pretty fast coderef, inlining all type constraint checks, etc.

moose_coercion

A Moose::Meta::TypeCoercion object equivalent to this one. Don't set this manually; rely on the default built one.

Methods

has_type_constraint

Predicate method.

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 object that overloads coderefification). In either case, the value to be coerced is $_.

coerce($value)

Coerce the value to the target type.

assert_coerce($value)

Coerce the value to the target type, and throw an exception if the result does not validate against the target type constraint.

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 would be actually be necessary for this type.

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 be actually be necessary for this value (due to it already meeting the target type constraint).

can_be_inlined

Returns true iff the coercion can be inlined.

inline_coercion($varname)

Much like inline_coerce from Type::Tiny.

isa($class), can($method), AUTOLOAD(@args)

If Moose is loaded, then the combination of these methods is used to mock a Moose::Meta::TypeCoercion.

Overloading

  • Boolification is overloaded to always return true.

  • Coderefification is overloaded to call coerce.

  • On Perl 5.10.1 and above, smart match is overloaded to call has_coercion_for_value.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.

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

COPYRIGHT AND LICENCE

This software is copyright (c) 2013 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.

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.