NAME

Venus::Coercion - Coercion Class

ABSTRACT

Coercion Class for Perl 5

SYNOPSIS

package main;

use Venus::Coercion;

my $coercion = Venus::Coercion->new;

# $coercion->accept('float');

# $coercion->format(sub{sprintf '%.2f', $_});

# $coercion->result(123.456);

# 123.46

DESCRIPTION

This package provides a mechanism for evaluating type coercions on data. Built-in type coercions are handled via Venus::Check.

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Buildable

METHODS

This package provides the following methods:

accept

accept(string $name, any @args) (Venus::Coercion)

The accept method registers a condition via "check" based on the arguments provided. The built-in types are defined as methods in Venus::Check.

Since 3.55

accept example 1
# given: synopsis

package main;

$coercion = $coercion->accept('float');

# bless(..., "Venus::Coercion")

# $coercion->result;

# undef

# $coercion->result(1.01);

# 1.01
accept example 2
# given: synopsis

package main;

$coercion = $coercion->accept('number');

# bless(..., "Venus::Coercion")

# $coercion->result(1.01);

# 1.01

# $coercion->result(1_01);

# 101
accept example 3
# given: synopsis

package Example1;

sub new {
  bless {};
}

package Example2;

sub new {
  bless {};
}

package main;

$coercion = $coercion->accept('object');

# bless(..., "Venus::Coercion")

# $coercion->result;

# undef

# $coercion->result(qr//);

# qr//

# $coercion->result(Example1->new);

# bless(..., "Example1")

# $coercion->result(Example2->new);

# bless(..., "Example2")
accept example 4
# given: synopsis

package Example1;

sub new {
  bless {};
}

package Example2;

sub new {
  bless {};
}

package main;

$coercion = $coercion->accept('Example1');

# bless(..., "Venus::Coercion")

# $coercion->result;

# undef

# $coercion->result(qr//);

# qr//

# $coercion->result(Example1->new);

# bless(..., "Example1")

# $coercion->result(Example2->new);

# bless(..., "Example2")

check

check(Venus::Check $data) (Venus::Check)

The check method gets or sets the Venus::Check object used for performing runtime data type validation.

Since 3.55

check example 1
# given: synopsis

package main;

my $check = $coercion->check(Venus::Check->new);

# bless(..., 'Venus::Check')
check example 2
# given: synopsis

package main;

$coercion->check(Venus::Check->new);

my $check = $coercion->check;

# bless(..., 'Venus::Check')

clear

clear() (Venus::Coercion)

The clear method resets the "check" attributes and returns the invocant.

Since 3.55

clear example 1
# given: synopsis

package main;

$coercion->accept('string');

$coercion = $coercion->clear;

# bless(..., "Venus::Coercion")

eval

eval(any $data) (boolean)

The eval method dispatches to the "check" object as well as evaluating any custom conditions, and returns the coerced value if all conditions pass, and the original value if any condition fails.

Since 3.55

eval example 1
# given: synopsis

package main;

use Venus::Float;

$coercion->accept('float');

$coercion->format(sub{Venus::Float->new($_)});

my $eval = $coercion->eval('1.00');

# bless(..., "Venus::Float")
eval example 2
# given: synopsis

package main;

use Venus::Float;

$coercion->accept('float');

$coercion->format(sub{Venus::Float->new($_)});

my $eval = $coercion->eval(1_00);

# 100

evaler

evaler(any @args) (coderef)

The evaler method returns a coderef which calls the "eval" method with the invocant when called.

Since 3.55

evaler example 1
# given: synopsis

package main;

my $evaler = $coercion->evaler;

# sub{...}

# my $result = $evaler->();

# undef
evaler example 2
# given: synopsis

package main;

my $evaler = $coercion->accept('any')->evaler;

# sub{...}

# my $result = $evaler->('hello');

# "hello"

format

format(coderef $code) (Venus::Coercion)

The format method registers a custom (not built-in) coercion condition and returns the invocant.

Since 3.55

format example 1
# given: synopsis

package main;

$coercion->accept('either', 'float', 'number');

my $format = $coercion->format(sub {
  int $_
});

# bless(.., "Venus::Coercion")

result

result(any $data) (boolean)

The result method dispatches to the "eval" method and returns the result.

Since 3.55

result example 1
# given: synopsis

package main;

$coercion->accept('float');

$coercion->format(sub{int $_});

my $result = $coercion->result('1.00');

# 1
result example 2
# given: synopsis

package main;

$coercion->accept('float');

$coercion->format(sub{int $_});

my $result = $coercion->result('0.99');

# 0

AUTHORS

Awncorp, awncorp@cpan.org

LICENSE

Copyright (C) 2000, Awncorp, awncorp@cpan.org.

This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.