Specio::Coercion - A class representing a coercion from one type to another
version 0.47
| Use Case | Command | Description |
|---|---|---|
| Create a coercion from a type | Specio::Coercion->new( from => $from_type, to => $to_type, coercion => sub { ... } ) | đĻ Create a new coercion object with a subroutine reference |
| Create an inline coercion | Specio::Coercion->new( from => $from_type, to => $to_type, inline_generator => sub { ... } ) | ⥠Create a coercion with an inline generator for faster execution |
| Coerce a value | $coercion->coerce_value(42) | đ Apply the coercion to a value |
| Get inline code | $coercion->inline_coercion('$_[0]') | đ Generate string of code for inlining the coercion |
| Check if can be inlined | $coercion->can_be_inlined() | â Returns true if the coercion has an inline generator and the source type can be inlined |
| Clone a coercion | $coercion->clone() | đ Returns a copy of the coercion object |
| Clone with new destination type | $coercion->clone_with_new_to($new_type) | đ Clone the coercion, replacing the to type |
my $coercion = $type->coercion_from_type('Int');
my $new_value = $coercion->coerce_value(42);
if ( $coercion->can_be_inlined() ) {
my $code = $coercion->inline_coercion('$_[0]');
}
This class represents a coercion from one type to another. Internally, a coercion is a piece of code that takes a value of one type returns a new value of a new type. For example, a coercion from Num to Int might round a number to its nearest integer and return that integer.
Coercions can be implemented either as a simple subroutine reference or as an inline generator subroutine. Using an inline generator is faster but more complicated.
This class provides the following methods.
This method creates a new coercion object. It accepts the following named parameters:
from => $typeto => $typecoercion => sub { ... }inline_generator.inline_generator parameter is required.using in the parameter list.inline_generator => sub { ... }'$_[0]' or '$var'.coercion.coercion parameter is required.inline in the parameter list.inline_environment => {}declared_at => $declared_atThese methods are all read-only attribute accessors for the corresponding attribute.
This returns a string describing the coercion. This includes the names of the to and from type and where the coercion was declared, so you end up with something like 'coercion from Foo to Bar declared in package My::Lib (lib/My/Lib.pm) at line 42'.
Given a value of the right from type, returns a new value of the to type.
This method does not actually check that the types of given or return values.
Given a variable name like '$_[0]' this returns a string with code for the coercion.
Note that this method will die if the coercion does not have an inline generator.
This returns true if the coercion has an inline generator and the constraint it is from can be inlined. This exists primarily for the benefit of the inline_coercion_and_check() method for type constraint object.
This returns a hash defining the variables that need to be closed over when inlining the coercion. The keys are full variable names like '$foo' or '@bar'. The values are references to a variable of the matching type.
Returns a clone of this object.
This returns a clone of the coercion, replacing the to type with a new one. This is intended for use when the to type itself is being cloned as part of importing that type. We need to make sure the newly cloned coercion has the newly cloned type as well.
This class does the Specio::Role::Inlinable role.
Bugs may be submitted at <https://github.com/houseabsolute/Specio/issues>.
I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".
The source code repository for Specio can be found at <https://github.com/houseabsolute/Specio>.
Dave Rolsky <autarch AT urth.org>
This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the LICENSE file included with this distribution.
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-13 00:30 @216.73.217.60
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)