Perl Diver 2.31
Main Environment Variables Perl Default Values Perl Config - Summary Perl Config - Full Installed Modules List Directory uptime Docs

Module Documentation
Details and documentation about a specific module, including version and documentation (if available). Note that while links to perldoc.com and search.cpan.org are provided, the module may be part of a larger distribution. If you reach a File Not Found page on either site, please try the parent module.

Specio::Coercion

Name Specio::Coercion
Version 0.42
Located at /usr/share/perl5/vendor_perl
File /usr/share/perl5/vendor_perl/Specio/Coercion.pm
Is Core No
Search CPAN for this module Specio::Coercion
Documentation Specio::Coercion
Module Details Specio::Coercion


NAME

Specio::Coercion - A class representing a coercion from one type to another


VERSION

version 0.42


SYNOPSIS

    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]');
    }


DESCRIPTION

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


API

This class provides the following methods.

Specio::Coercion->new( ... )

This method creates a new coercion object. It accepts the following named parameters:

$coercion->from(), $coercion->to(), $coercion->declared_at()

These methods are all read-only attribute accessors for the corresponding attribute.

$coercion->description

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

$coercion->coerce($value)

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.

$coercion->inline_coercion($var)

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.

$coercion->can_be_inlined()

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.

$coercion->inline_environment()

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.

$coercion->clone()

Returns a clone of this object.

$coercion->clone_with_new_to($new_to_type)

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.


ROLES

This class does the the Specio::Role::Inlinable manpage role.


SUPPORT

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.


SOURCE

The source code repository for Specio can be found at https://github.com/houseabsolute/Specio.


AUTHOR

Dave Rolsky <autarch@urth.org>


COPYRIGHT AND LICENSE

This software is Copyright (c) 2012 - 2017 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.

Perl Diver brought to you by ScriptSolutions.com © 1997- 2024