| Type::Registry(3) | User Contributed Perl Documentation | Type::Registry(3) |
Type::Registry - a glorified hashref for looking up type constraints
package Foo::Bar;
use Type::Registry;
my $reg = "Type::Registry"->for_me; # a registry for Foo::Bar
# Register all types from Types::Standard
$reg->add_types(-Standard);
# Register just one type from Types::XSD
$reg->add_types(-XSD => ["NonNegativeInteger"]);
# Register all types from MyApp::Types
$reg->add_types("MyApp::Types");
# Create a type alias
$reg->alias_type("NonNegativeInteger" => "Count");
# Look up a type constraint
my $type = $reg->lookup("ArrayRef[Count]");
$type->check([1, 2, 3.14159]); # croaks
Alternatively:
package Foo::Bar;
use Type::Registry qw( t );
# Register all types from Types::Standard
t->add_types(-Standard);
# Register just one type from Types::XSD
t->add_types(-XSD => ["NonNegativeInteger"]);
# Register all types from MyApp::Types
t->add_types("MyApp::Types");
# Create a type alias
t->alias_type("NonNegativeInteger" => "Count");
# Look up a type constraint
my $type = t("ArrayRef[Count]");
$type->check([1, 2, 3.14159]); # croaks
This module is covered by the Type-Tiny stability policy.
A type registry is basically just a hashref mapping type names to type constraint objects.
Note that any type constraint you have imported from Type::Library-based type libraries will be automatically available in your class' registry.
Strings are the names of type libraries; if the first character is a hyphen, it is expanded to the "Types::" prefix. If followed by an arrayref, this is the list of types to import from that library. Otherwise, imports all types from the library.
use Type::Registry qw(t);
t->add_types(-Standard); # OR: t->add_types("Types::Standard");
t->add_types(
-TypeTiny => ['HashLike'],
-Standard => ['HashRef' => { -as => 'RealHash' }],
);
MooseX::Types (and experimentally, MouseX::Types) libraries can also be added this way, but cannot be followed by an arrayref of types to import.
This method can even add MooseX::Types and MouseX::Types type constraints; indeed anything that can be handled by Types::TypeTiny's "to_TypeTiny" function. (Bear in mind that to_TypeTiny always results in an anonymous type constraint, so $name will be required.)
Returns undef if not found.
t->lookup("Int|ArrayRef[Int]")
The DSL can be summed up as:
X type from this registry
My::Lib::X type from a type library
~X complementary type
X | Y union
X & Y intersection
X[...] parameterized type
slurpy X slurpy type
Foo::Bar:: class type
Croaks if not found.
$registry->Str; # like $registry->lookup("Str")
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
Type::Library.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013-2014, 2017-2020 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
| 2020-10-28 | perl v5.34.0 |