Venus::Coercion - Coercion Class
Coercion Class for Perl 5
package main;
use Venus::Coercion;
my $coercion = Venus::Coercion->new;
# $coercion->accept('float');
# $coercion->format(sub{sprintf '%.2f', $_});
# $coercion->result(123.456);
# 123.46
This package provides a mechanism for evaluating type coercions on
data. Built-in type coercions are handled via Venus::Check.
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Buildable
This package provides the following methods:
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(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() (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(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(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(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(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
Awncorp, "awncorp@cpan.org"
Copyright (C) 2022, 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.