Venus::Constraint - Constraint Class
Constraint Class for Perl 5
package main;
use Venus::Constraint;
my $constraint = Venus::Constraint->new;
# $constraint->accept('float');
# $constraint->ensure(sub{$_ > 1});
# $constraint->result(1.01);
# true
This package provides a mechanism for evaluating type constraints
on data. Built-in type constraints 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::Constraint)
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;
$constraint = $constraint->accept('float');
# bless(..., "Venus::Constraint")
# $constraint->result;
# false
# $constraint->result(1.01);
# true
- accept example 2
-
# given: synopsis
package main;
$constraint = $constraint->accept('number');
# bless(..., "Venus::Constraint")
# $constraint->result(1.01);
# false
# $constraint->result(1_01);
# true
- accept example 3
-
# given: synopsis
package Example1;
sub new {
bless {};
}
package Example2;
sub new {
bless {};
}
package main;
$constraint = $constraint->accept('object');
# bless(..., "Venus::Constraint")
# $constraint->result;
# false
# $constraint->result(qr//);
# false
# $constraint->result(Example1->new);
# true
# $constraint->result(Example2->new);
# true
- accept example 4
-
# given: synopsis
package Example1;
sub new {
bless {};
}
package Example2;
sub new {
bless {};
}
package main;
$constraint = $constraint->accept('Example1');
# bless(..., "Venus::Constraint")
# $constraint->result;
# false
# $constraint->result(qr//);
# false
# $constraint->result(Example1->new);
# true
# $constraint->result(Example2->new);
# false
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 = $constraint->check(Venus::Check->new);
# bless(..., 'Venus::Check')
- check example 2
-
# given: synopsis
package main;
$constraint->check(Venus::Check->new);
my $check = $constraint->check;
# bless(..., 'Venus::Check')
clear() (Venus::Constraint)
The clear method resets the "check" attributes and
returns the invocant.
Since 3.55
- clear example 1
-
# given: synopsis
package main;
$constraint->accept('string');
$constraint = $constraint->clear;
# bless(..., "Venus::Constraint")
ensure(coderef $code) (Venus::Constraint)
The ensure method registers a custom (not built-in) constraint
condition and returns the invocant.
Since 3.55
- ensure example 1
-
# given: synopsis
package main;
$constraint->accept('number');
my $ensure = $constraint->ensure(sub {
$_ >= 0
});
# bless(.., "Venus::Constraint")
- ensure example 2
-
# given: synopsis
package main;
$constraint->accept('number');
my $ensure = $constraint->ensure(sub {
my ($source, $value) = @_;
if ($value >= 0) {
return 1;
}
else {
return 0;
}
});
# bless(..., "Venus::Constraint")
eval(any $data) (boolean)
The eval method dispatches to the "check" object as well
as evaluating any custom conditions, and returns true if all conditions
pass, and false if any condition fails.
Since 3.55
- eval example 1
-
# given: synopsis
package main;
$constraint->accept('float');
$constraint->ensure(sub{$_ >= 1});
my $eval = $constraint->eval('1.00');
# true
- eval example 2
-
# given: synopsis
package main;
$constraint->accept('float');
$constraint->ensure(sub{$_ >= 1});
my $eval = $constraint->eval('0.99');
# false
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 = $constraint->evaler;
# sub{...}
# my $result = $evaler->();
# false
- evaler example 2
-
# given: synopsis
package main;
my $evaler = $constraint->accept('any')->evaler;
# sub{...}
# my $result = $evaler->();
# true
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;
$constraint->accept('float');
$constraint->ensure(sub{$_ >= 1});
my $result = $constraint->result('1.00');
# true
- result example 2
-
# given: synopsis
package main;
$constraint->accept('float');
$constraint->ensure(sub{$_ >= 1});
my $result = $constraint->result('0.99');
# false
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.