Specio::Constraint::Simple - Online Linux Manual PageSection : 3
Updated : 2022-01-21
Source : perl v5.34.0
Note : User Contributed Perl Documentation

NAMESpecio::Constraint::Simple − Class for simple (non−parameterized or specialized) types

VERSIONversion 0.47

SYNOPSIS​ 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]');

DESCRIPTIONThis class implements simple type constraints, constraints without special properties or parameterization. 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.

APIThis class provides the following methods.

Specio::Constraint::Simple−>new(...)This creates a new constraint. It accepts the following named parameters: • name => $name This is the type's name. The name is optional, but if provided it must be a string. • parent => $type The type's parent type. This must be an object which does the Specio::Constraint::Role::Interface role. This parameter is optional. • constraint => sub { ... } 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. • 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 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. • 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 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. • message_generator => sub { ... } 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. • declared_at => $declared_at 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.

$type>nameReturns the name of the type as it was passed the constructor.

$type>parentReturns the parent type passed to the constructor. If the type has no parent this returns undef.

$type>is_anonReturns false for named types, true otherwise.

$type>is_a_type_of($other_type)Given a type object, this returns true if the type this method is called on is a descendant of that type or is that type.

$type>is_same_type_as($other_type)Given a type object, this returns true if the type this method is called on is the same as that type.

$type>coercionsReturns a list of Specio::Coercion objects which belong to this constraint.

$type>coercion_from_type($name)Given a type name, this method returns a Specio::Coercion object which coerces from that type, if such a coercion exists.

$type>validate_or_die($value)This method does nothing if the value is valid. If it is not, it throws a Specio::Exception.

$type>value_is_valid($value)Returns true or false depending on whether the $value passes the type constraint.

$type>has_real_constraintThis returns true if the type was created with a constraint or ​inline_generator parameter. This is used internally to skip type checks for types that don't actually implement a constraint.

$type>descriptionThis returns a string describing the type. This includes the type's name and where it was declared, so you end up with something like 'type named Foo declared in package My::Lib (lib/My/Lib.pm) at line 42'. If the type is anonymous the name will be anonymous type.

$type>idThis is a unique id for the type as a string. This is useful if you need to make a hash key based on a type, for example. This should be treated as an essentially arbitrary and opaque string, and could change at any time in the future. If you want something human-readable, use the $type−>description method.

$type>add_coercion($coercion)This adds a new Specio::Coercion to the type. If the type already has a coercion from the same type as the new coercion, it will throw an error.

$type>has_coercion_from_type($other_type)This method returns true if the type can coerce from the other type.

$type>coerce_value($value)This attempts to coerce a value into a new value that matches the type. It checks all of the type's coercions. If it finds one which has a from type that accepts the value, it runs the coercion and returns the new value. If it cannot find a matching coercion it returns the original value.

$type>inline_coercion_and_check($var)Given a variable name, this returns a string of code and an environment hash that implements all of the type's coercions as well as the type check itself. 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.

$type>inline_assert($var)Given a variable name, this generates code that implements the constraint and throws an exception if the variable does not pass the constraint. 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.

$type>inline_check($var)Given a variable name, this returns a string of code that implements the constraint. If the type is not inlinable, this method throws an error.

$type>inline_coercion($var)Given a variable name, this returns a string of code and an environment hash that implements all of the type's coercions. It does not check that the resulting value is valid. 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.

$type>inline_environment()This returns a hash defining the variables that need to be closed over when inlining the type. The keys are full variable names like '$foo' or ​'@bar'. The values are references to a variable of the matching type.

$type>coercion_subThis method returns a sub ref that takes a single argument and applied all relevant coercions to it. This sub ref will use Sub::Quote if all the type's coercions are inlinable. This method exists primarily for the benefit of Moo.

OVERLOADINGAll constraints implement the following overloads:

Subroutine De-referencingThis is done for the benefit of Moo. The returned subroutine uses Sub::Quote if the type constraint is inlinable.

StringificationFor non-anonymous types, this will be the type's name. For anonymous types, a string like _ _ANON_ _(Str) is generated. However, this string should not be expected to be stable across releases, so don't use it for things like equality checks!

BoolificationThis always returns true.

String Equality (eq)This calls $type−>is_same_type_as($other) to compare the two types.

ROLESThis role does the Specio::Constraint::Role::Interface and Specio::Role::Inlinable roles.

SUPPORTBugs may be submitted at <https://github.com/houseabsolute/Specio/issues>. I am also usually active on IRC as 'autarch' on irc://irc.perl.org.

SOURCEThe source code repository for Specio can be found at <https://github.com/houseabsolute/Specio>.

AUTHORDave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSEThis software is Copyright (c) 2012 − 2021 by Dave Rolsky. 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.
0
Johanes Gumabo
Data Size   :   33,497 byte
man-Specio::Constraint::Simple.3pmBuild   :   2024-12-05, 20:55   :  
Visitor Screen   :   x
Visitor Counter ( page / site )   :   3 / 190,105
Visitor ID   :     :  
Visitor IP   :   18.227.46.87   :  
Visitor Provider   :   AMAZON-02   :  
Provider Position ( lat x lon )   :   39.962500 x -83.006100   :   x
Provider Accuracy Radius ( km )   :   1000   :  
Provider City   :   Columbus   :  
Provider Province   :   Ohio ,   :   ,
Provider Country   :   United States   :  
Provider Continent   :   North America   :  
Visitor Recorder   :   Version   :  
Visitor Recorder   :   Library   :  
Online Linux Manual Page   :   Version   :   Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page   :   Library   :   lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page   :   Library   :   lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base   :   Version   :   Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base   :   Library   :   lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36

Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.

ERROR : Need New Coding :         (parse_manual_page_|249|Specio::Constraint::Simple.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\| )         (htmlprn|149|Specio::Constraint::Simple.3pm|36/37|.el══─{─══. ds --  —  |.el══─{─══. ds -- \|\(em\| )         (parse_manual_page_|249|Specio::Constraint::Simple.3pm|43|br══─}─══|'br══─}─══ )         (htmlprn|149|Specio::Constraint::Simple.3pm|43|'br══─}─══ |'br══─}─══ )