Dave Rolsky > Specio-0.08 > Specio::Constraint::Simple



Annotate this POD


Open  1
View/Report Bugs
Module Version: 0.08   Source   Latest Release: Specio-0.24


Specio::Constraint::Simple - Class for simple (non-parameterized or specialized) types


version 0.08


  my $str = t('Str');

  print $str->name(); # Str

  my $parent = $str->parent();

  if ( $str->value_is_valid($value) ) { ... }


  my $code = $str->inline_coercion_and_check('$_[0]');


This 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.


This class provides the following methods.


This creates a new constraint. It accepts the following named parameters:

It is possible to create a type without a constraint of its own.

$type->name(), $type->parent()

Returns the value of these parameters as they were passed to the constructor.


Returns false for named types, true otherwise.


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.


Given a type object, this returns true if the type this method is called on is the same as that type.


Returns a list of Specio::Coercion objects which belong to this constraint.


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


This method does nothing if the value is valid. If it is not, it throws a Specio::Exception.


Returns true or false depending on whether the $value passes the type constraint.


This 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.


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.


This 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 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.


This method returns true if the type can coerce from the other type.


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.


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.


This role does the Specio::Constraint::Role::Interface, Specio::Role::Inlinable and MooseX::Clone roles.


Dave Rolsky <autarch@urth.org>


This software is Copyright (c) 2013 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)
syntax highlighting: