Dave Rolsky > Specio-0.11 > Specio::Coercion

Download:
Specio-0.11.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.11   Source  

NAME ^

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

VERSION ^

version 0.11

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->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->clone()

Returns a clone of this object.

ROLES ^

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

AUTHOR ^

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE ^

This software is Copyright (c) 2014 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)
syntax highlighting: