GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  SPECIO::COERCION (3)

.ds Aq ’

NAME

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

CONTENTS

VERSION

version 0.12

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:
o from => $type

The type this coercion is from. The type must be an object which does the Specio::Constraint::Role::Interface interface.

This parameter is required.

o to => $type

The type this coercion is to. The type must be an object which does the Specio::Constraint::Role::Interface interface.

This parameter is required.

o coercion => sub { ... }

A subroutine reference implementing the coercion. It will be called as a method on the object and passed a single argument, the value to coerce.

It should return the new value.

This parameter is mutually exclusive with inline_generator.

Either this parameter or the inline_generator parameter is required.

You can also pass this option with the key using in the parameter list.

o inline_generator => sub { ... }

This should be a subroutine reference which returns a string containing a single term. This code should not end in a semicolon. This code should implement the coercion.

The generator will be called as a method on the coercion with a single argument. That argument is the name of the variable being coerced, something like $_[0] or $var.

This parameter is mutually exclusive with coercion.

Either this parameter or the coercion parameter is required.

You can also pass this option with the key inline in the parameter list.

o inline_environment => {}

This should be a hash reference of variable names (with sigils) and values for that variable. The values should be references to the values of the variables.

This environment will be used when compiling the coercion as part of a subroutine. The named variables will be captured as closures in the generated subroutine, using Eval::Closure.

It should be very rare to need to set this in the constructor. It’s more likely that a special coercion subclass would need to provide values that it generates internally.

This parameter defaults to an empty hash reference.

o declared_at => $declared_at

This parameter must be a Specio::DeclaredAt object.

This parameter is required.

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

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

CW$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.

CW$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.

CW$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.

CW$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) 2015 by Dave Rolsky.

This is free software, licensed under:



  The Artistic License 2.0 (GPL Compatible)



Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 SPECIO::COERCION (3) 2015-12-19

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.