{
    "mode": "perldoc",
    "parameter": "Specio::Coercion",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Specio%3A%3ACoercion/json",
    "generated": "2026-06-11T15:56:20Z",
    "synopsis": "my $coercion = $type->coercionfromtype('Int');\nmy $newvalue = $coercion->coercevalue(42);\nif ( $coercion->canbeinlined() ) {\nmy $code = $coercion->inlinecoercion('$[0]');\n}",
    "sections": {
        "NAME": {
            "content": "Specio::Coercion - A class representing a coercion from one type to another\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 0.47\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my $coercion = $type->coercionfromtype('Int');\n\nmy $newvalue = $coercion->coercevalue(42);\n\nif ( $coercion->canbeinlined() ) {\nmy $code = $coercion->inlinecoercion('$[0]');\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This class represents a coercion from one type to another. Internally, a coercion is a piece of\ncode that takes a value of one type returns a new value of a new type. For example, a coercion\nfrom c<Num> to \"Int\" might round a number to its nearest integer and return that integer.\n\nCoercions can be implemented either as a simple subroutine reference or as an inline generator\nsubroutine. Using an inline generator is faster but more complicated.\n",
            "subsections": []
        },
        "API": {
            "content": "This class provides the following methods.\n\nSpecio::Coercion->new( ... )\nThis method creates a new coercion object. It accepts the following named parameters:\n\n*   from => $type\n\nThe type this coercion is from. The type must be an object which does the\nSpecio::Constraint::Role::Interface interface.\n\nThis parameter is required.\n\n*   to => $type\n\nThe type this coercion is to. The type must be an object which does the\nSpecio::Constraint::Role::Interface interface.\n\nThis parameter is required.\n\n*   coercion => sub { ... }\n\nA subroutine reference implementing the coercion. It will be called as a method on the\nobject and passed a single argument, the value to coerce.\n\nIt should return the new value.\n\nThis parameter is mutually exclusive with \"inlinegenerator\".\n\nEither this parameter or the \"inlinegenerator\" parameter is required.\n\nYou can also pass this option with the key \"using\" in the parameter list.\n\n*   inlinegenerator => sub { ... }\n\nThis should be a subroutine reference which returns a string containing a single term. This\ncode should *not* end in a semicolon. This code should implement the coercion.\n\nThe generator will be called as a method on the coercion with a single argument. That\nargument is the name of the variable being coerced, something like '$[0]' or '$var'.\n\nThis parameter is mutually exclusive with \"coercion\".\n\nEither this parameter or the \"coercion\" parameter is required.\n\nYou can also pass this option with the key \"inline\" in the parameter list.\n\n*   inlineenvironment => {}\n\nThis should be a hash reference of variable names (with sigils) and values for that\nvariable. The values should be *references* to the values of the variables.\n\nThis environment will be used when compiling the coercion as part of a subroutine. The named\nvariables will be captured as closures in the generated subroutine, using Eval::Closure.\n\nIt should be very rare to need to set this in the constructor. It's more likely that a\nspecial coercion subclass would need to provide values that it generates internally.\n\nThis parameter defaults to an empty hash reference.\n\n*   declaredat => $declaredat\n\nThis parameter must be a Specio::DeclaredAt object.\n\nThis parameter is required.\n\n$coercion->from(), $coercion->to(), $coercion->declaredat()\nThese methods are all read-only attribute accessors for the corresponding attribute.\n\n$coercion->description\nThis returns a string describing the coercion. This includes the names of the to and from type\nand where the coercion was declared, so you end up with something like \"'coercion from Foo to\nBar declared in package My::Lib (lib/My/Lib.pm) at line 42'\".\n\n$coercion->coerce($value)\nGiven a value of the right \"from\" type, returns a new value of the \"to\" type.\n\nThis method does not actually check that the types of given or return values.\n\n$coercion->inlinecoercion($var)\nGiven a variable name like '$[0]' this returns a string with code for the coercion.\n\nNote that this method will die if the coercion does not have an inline generator.\n\n$coercion->canbeinlined()\nThis returns true if the coercion has an inline generator *and* the constraint it is from can be\ninlined. This exists primarily for the benefit of the \"inlinecoercionandcheck()\" method for\ntype constraint object.\n\n$coercion->inlineenvironment()\nThis returns a hash defining the variables that need to be closed over when inlining the\ncoercion. The keys are full variable names like '$foo' or '@bar'. The values are *references* to\na variable of the matching type.\n\n$coercion->clone()\nReturns a clone of this object.\n\n$coercion->clonewithnewto($newtotype)\nThis returns a clone of the coercion, replacing the \"to\" type with a new one. This is intended\nfor use when the to type itself is being cloned as part of importing that type. We need to make\nsure the newly cloned coercion has the newly cloned type as well.\n",
            "subsections": []
        },
        "ROLES": {
            "content": "This class does the Specio::Role::Inlinable role.\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "Bugs may be submitted at <https://github.com/houseabsolute/Specio/issues>.\n\nI am also usually active on IRC as 'autarch' on \"irc://irc.perl.org\".\n",
            "subsections": []
        },
        "SOURCE": {
            "content": "The source code repository for Specio can be found at <https://github.com/houseabsolute/Specio>.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Dave Rolsky <autarch@urth.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is Copyright (c) 2012 - 2021 by Dave Rolsky.\n\nThis is free software, licensed under:\n\nThe Artistic License 2.0 (GPL Compatible)\n\nThe full text of the license can be found in the LICENSE file included with this distribution.\n",
            "subsections": []
        }
    },
    "summary": "Specio::Coercion - A class representing a coercion from one type to another",
    "flags": [],
    "examples": [],
    "see_also": []
}