my $str = t('Str');
print $str->name; # Str
my $parent = $str->parent;
if ( $str->value_is_valid($value) ) { ... }
$str->validate_or_die($value);
my $code = $str->inline_coercion_and_check('$_[0]');
It does not actually contain any real code of its own. The entire implementation is provided by the Specio::Constraint::Role::Interface role, but the primary API for type constraints is documented here.
All other type constraint classes in this distribution implement this API, except where otherwise noted.
This is the type's name. The name is optional, but if provided it must be a string.
The type's parent type. This must be an object which does the Specio::Constraint::Role::Interface role.
This parameter is optional.
A subroutine reference implementing the constraint. It will be called as a method on the object and passed a single argument, the value to check.
It should return true or false to indicate whether the value matches the constraint.
This parameter is mutually exclusive with "inline_generator".
You can also pass this option with the key "where" in the parameter list.
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 constraint.
The generator will be called as a method on the constraint with a single argument. That argument is the name of the variable being coerced, something like '$_[0]' or '$var'.
The inline generator is expected to include code to implement both the current type and all its parents. Typically, the easiest way to do this is to write a subroutine something like this:
sub {
my $self = shift;
my $var = shift;
return $_[0]->parent->inline_check( $_[1] )
. ' and more checking code goes here';
}
This parameter is mutually exclusive with "constraint".
You can also pass this option with the key "inline" in the parameter list.
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 constraint 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 type subclass would need to provide values that it generates internally.
If you do set this, you are responsible for generating variable names that won't clash with anything else in the inlined code.
This parameter defaults to an empty hash reference.
A subroutine to generate an error message when the type check fails. The default message says something like ``Validation failed for type named Int declared in package Specio::Library::Builtins (.../Specio/blib/lib/Specio/Library/Builtins.pm) at line 147 in sub named (eval) with value 1.1''.
You can override this to provide something more specific about the way the type failed.
The subroutine you provide will be called as a subroutine, not as a method, with two arguments. The first is the description of the type (the bit in the message above that starts with ``type named Int ...'' and ends with ``... in sub named (eval)''. This description says what the thing is and where it was defined.
The second argument is the value that failed the type check, after any coercions that might have been applied.
You can also pass this option with the key "message" in the parameter list.
This parameter must be a Specio::DeclaredAt object.
This parameter is required.
It is possible to create a type without a constraint of its own.
If it cannot find a matching coercion it returns the original value.
This will throw an exception unless both the type and all of its coercions are inlinable.
The generated code will throw a Specio::Exception if the type constraint fails. If the constraint passes, then the generated code returns the (possibly coerced) value.
The return value is a two-element list. The first element is the code. The second is a hash reference containing variables which need to be in scope for the code to work. This is intended to be passed to Eval::Closure's "eval_closure" subroutine.
The returned code is a single "do { }" block without a terminating semicolon.
The return value is a two-element list. The first element is the code. The second is a hash reference containing variables which need to be in scope for the code to work. This is intended to be passed to Eval::Closure's "eval_closure" subroutine.
This will throw an exception unless all of the type's coercions are inlinable.
The return value is a two-element list. The first element is the code. The second is a hash reference containing variables which need to be in scope for the code to work. This is intended to be passed to Eval::Closure's "eval_closure" subroutine.
The returned code is a single "do { }" block without a terminating semicolon.
This method exists primarily for the benefit of Moo.
I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the LICENSE file included with this distribution.