{
    "mode": "perldoc",
    "parameter": "Type::Coercion",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Type%3A%3ACoercion/json",
    "generated": "2026-06-10T16:25:57Z",
    "sections": {
        "NAME": {
            "content": "Type::Coercion - a set of coercions to a particular target type constraint\n",
            "subsections": []
        },
        "STATUS": {
            "content": "This module is covered by the Type-Tiny stability policy.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "",
            "subsections": [
                {
                    "name": "Constructors",
                    "content": "\"new(%attributes)\"\nMoose-style constructor function.\n\n\"add($c1, $c2)\"\nCreate a Type::Coercion from two existing Type::Coercion objects.\n"
                },
                {
                    "name": "Attributes",
                    "content": "Attributes are named values that may be passed to the constructor. For each attribute, there is\na corresponding reader method. For example:\n\nmy $c = Type::Coercion->new( typeconstraint => Int );\nmy $t = $c->typeconstraint;  # Int\n\nImportant attributes\nThese are the attributes you are likely to be most interested in providing when creating your\nown type coercions, and most interested in reading when dealing with coercion objects.\n\n\"typeconstraint\"\nWeak reference to the target type constraint (i.e. the type constraint which the output of\ncoercion coderefs is expected to conform to).\n\n\"typecoercionmap\"\nArrayref of source-type/code pairs.\n\n\"frozen\"\nBoolean; default false. A frozen coercion cannot have \"addtypecoercions\" called upon it.\n\n\"name\"\nA name for the coercion. These need to conform to certain naming rules (they must begin with\nan uppercase letter and continue using only letters, digits 0-9 and underscores).\n\nOptional; if not supplied will be an anonymous coercion.\n\n\"displayname\"\nA name to display for the coercion when stringified. These don't have to conform to any\nnaming rules. Optional; a default name will be calculated from the \"name\".\n\n\"library\"\nThe package name of the type library this coercion is associated with. Optional.\nInformational only: setting this attribute does not install the coercion into the package.\n\nAttributes related to parameterizable and parameterized coercions\nThe following attributes are used for parameterized coercions, but are not fully documented\nbecause they may change in the near future:\n\n\"coerciongenerator\"\n\"parameters\"\n\"parameterizedfrom\"\n\nLazy generated attributes\nThe following attributes should not be usually passed to the constructor; unless you're doing\nsomething especially unusual, you should rely on the default lazily-built return values.\n\n\"compiledcoercion\"\nCoderef to coerce a value ($[0]).\n\nThe general point of this attribute is that you should not set it, but rely on the\nlazily-built default. Type::Coerce will usually generate a pretty fast coderef, inlining all\ntype constraint checks, etc.\n\n\"moosecoercion\"\nA Moose::Meta::TypeCoercion object equivalent to this one. Don't set this manually; rely on\nthe default built one.\n"
                },
                {
                    "name": "Methods",
                    "content": "Predicate methods\nThese methods return booleans indicating information about the coercion. They are each tightly\nassociated with a particular attribute. (See \"Attributes\".)\n\n\"hastypeconstraint\", \"haslibrary\"\nSimple Moose-style predicate methods indicating the presence or absence of an attribute.\n\n\"isanon\"\nReturns true iff the coercion does not have a \"name\".\n\nThe following predicates are used for parameterized coercions, but are not fully documented\nbecause they may change in the near future:\n\n\"hascoerciongenerator\"\n\"hasparameters\"\n\"isparameterizable\"\n\"isparameterized\"\n\nCoercion\nThe following methods are used for coercing values to a type constraint:\n\n\"coerce($value)\"\nCoerce the value to the target type.\n\nReturns the coerced value, or the original value if no coercion was possible.\n\n\"assertcoerce($value)\"\nCoerce the value to the target type, and throw an exception if the result does not validate\nagainst the target type constraint.\n\nReturns the coerced value.\n\nCoercion code definition methods\nThese methods all return $self so are suitable for chaining.\n\n\"addtypecoercions($type1, $code1, ...)\"\nTakes one or more pairs of Type::Tiny constraints and coercion code, creating an ordered\nlist of source types and coercion codes.\n\nCoercion codes can be expressed as either a string of Perl code (this includes objects which\noverload stringification), or a coderef (or object that overloads coderefification). In\neither case, the value to be coerced is $.\n\n\"addtypecoercions($coercionobject)\" also works, and can be used to copy coercions from\nanother type constraint:\n\n$type->coercion->addtypecoercions($othertype->coercion)->freeze;\n\n\"freeze\"\nSets the \"frozen\" attribute to true. Called automatically by Type::Tiny sometimes.\n\n\"ireallywanttounfreeze\"\nIf you really want to unfreeze a coercion, call this method.\n\nDon't call this method. It will potentially lead to subtle bugs.\n\nThis method is considered unstable; future versions of Type::Tiny may alter its behaviour\n(e.g. to throw an exception if it has been detected that unfreezing this particular coercion\nwill cause bugs).\n\nParameterization\nThe following method is used for parameterized coercions, but is not fully documented because it\nmay change in the near future:\n\n\"parameterize(@params)\"\n\nType coercion introspection methods\nThese methods allow you to determine a coercion's relationship to type constraints:\n\n\"hascoercionfortype($sourcetype)\"\nReturns true iff this coercion has a coercion from the source type.\n\nReturns the special string \"0 but true\" if no coercion should actually be necessary for this\ntype. (For example, if a coercion coerces to a theoretical \"Number\" type, there is probably\nno coercion necessary for values that already conform to the \"Integer\" type.)\n\n\"hascoercionforvalue($value)\"\nReturns true iff the value could be coerced by this coercion.\n\nReturns the special string \"0 but true\" if no coercion would be actually be necessary for\nthis value (due to it already meeting the target type constraint).\n\nThe \"typeconstraint\" attribute provides a type constraint object for the target type constraint\nof the coercion. See \"Attributes\".\n\nInlining methods\nThe following methods are used to generate strings of Perl code which may be pasted into stringy\n\"eval\"uated subs to perform type coercions:\n\n\"canbeinlined\"\nReturns true iff the coercion can be inlined.\n\n\"inlinecoercion($varname)\"\nMuch like \"inlinecoerce\" from Type::Tiny.\n\nOther methods\n\"qualifiedname\"\nFor non-anonymous coercions that have a library, returns a qualified \"MyLib::MyCoercion\"\nsort of name. Otherwise, returns the same as \"name\".\n\n\"isa($class)\", \"can($method)\", \"AUTOLOAD(@args)\"\nIf Moose is loaded, then the combination of these methods is used to mock a\nMoose::Meta::TypeCoercion.\n\nThe following methods exist for Moose/Mouse compatibility, but do not do anything useful.\n\n\"compiletypecoercion\"\n\"meta\"\n"
                },
                {
                    "name": "Overloading",
                    "content": "*   Boolification is overloaded to always return true.\n\n*   Coderefification is overloaded to call \"coerce\".\n\n*   On Perl 5.10.1 and above, smart match is overloaded to call \"hascoercionforvalue\".\n\nPrevious versions of Type::Coercion would overload the \"+\" operator to call \"add\". Support for\nthis was dropped after 0.040.\n"
                }
            ]
        },
        "DIAGNOSTICS": {
            "content": "*Attempt to add coercion code to a Type::Coercion which has been frozen*\nType::Tiny type constraints are designed as immutable objects. Once you've created a\nconstraint, rather than modifying it you generally create child constraints to do what you\nneed.\n\nType::Coercion objects, on the other hand, are mutable. Coercion routines can be added at\nany time during the object's lifetime.\n\nSometimes Type::Tiny needs to freeze a Type::Coercion object to prevent this. In Moose and\nMouse code this is likely to happen as soon as you use a type constraint in an attribute.\n\nWorkarounds:\n\n*   Define as many of your coercions as possible within type libraries, not within the code\nthat uses the type libraries. The type library will be evaluated relatively early,\nlikely before there is any reason to freeze a coercion.\n\n*   If you do need to add coercions to a type within application code outside the type\nlibrary, instead create a subtype and add coercions to that. The \"pluscoercions\" method\nprovided by Type::Tiny should make this simple.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Please report any bugs to <https://github.com/tobyink/p5-type-tiny/issues>.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Type::Tiny::Manual.\n\nType::Tiny, Type::Library, Type::Utils, Types::Standard.\n\nType::Coercion::Union.\n\nMoose::Meta::TypeCoercion.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Toby Inkster <tobyink@cpan.org>.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENCE": {
            "content": "This software is copyright (c) 2013-2014, 2017-2021 by Toby Inkster.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        },
        "DISCLAIMER OF WARRANTIES": {
            "content": "THIS PACKAGE IS PROVIDED \"AS IS\" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\nWITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.\n",
            "subsections": []
        }
    },
    "summary": "Type::Coercion - a set of coercions to a particular target type constraint",
    "flags": [],
    "examples": [],
    "see_also": []
}