1Type::Registry(3)     User Contributed Perl Documentation    Type::Registry(3)
2
3
4

NAME

6       Type::Registry - a glorified hashref for looking up type constraints
7

SYNOPSIS

9          package Foo::Bar;
10
11          use Type::Registry;
12
13          my $reg = "Type::Registry"->for_me;  # a registry for Foo::Bar
14
15          # Register all types from Types::Standard
16          $reg->add_types(-Standard);
17
18          # Register just one type from Types::XSD
19          $reg->add_types(-XSD => ["NonNegativeInteger"]);
20
21          # Register all types from MyApp::Types
22          $reg->add_types("MyApp::Types");
23
24          # Create a type alias
25          $reg->alias_type("NonNegativeInteger" => "Count");
26
27          # Look up a type constraint
28          my $type = $reg->lookup("ArrayRef[Count]");
29
30          $type->check([1, 2, 3.14159]);  # croaks
31
32       Alternatively:
33
34          package Foo::Bar;
35
36          use Type::Registry qw( t );
37
38          # Register all types from Types::Standard
39          t->add_types(-Standard);
40
41          # Register just one type from Types::XSD
42          t->add_types(-XSD => ["NonNegativeInteger"]);
43
44          # Register all types from MyApp::Types
45          t->add_types("MyApp::Types");
46
47          # Create a type alias
48          t->alias_type("NonNegativeInteger" => "Count");
49
50          # Look up a type constraint
51          my $type = t("ArrayRef[Count]");
52
53          $type->check([1, 2, 3.14159]);  # croaks
54

STATUS

56       This module is covered by the Type-Tiny stability policy.
57

DESCRIPTION

59       A type registry is basically just a hashref mapping type names to type
60       constraint objects.
61
62   Constructors
63       "new"
64           Create a new glorified hashref.
65
66       for_class($class)
67           Create or return the existing glorified hashref associated with the
68           given class.
69
70           Note that any type constraint you have imported from
71           Type::Library-based type libraries will be automatically available
72           in your class' registry.
73
74       "for_me"
75           Create or return the existing glorified hashref associated with the
76           caller.
77
78   Methods
79       add_types(@libraries)
80           The libraries list is treated as an "optlist" (a la Data::OptList).
81
82           Strings are the names of type libraries; if the first character is
83           a hyphen, it is expanded to the "Types::" prefix. If followed by an
84           arrayref, this is the list of types to import from that library.
85           Otherwise, imports all types from the library.
86
87              use Type::Registry qw(t);
88
89              t->add_types(-Standard);  # OR: t->add_types("Types::Standard");
90
91              t->add_types(
92                 -TypeTiny => ['HashLike'],
93                 -Standard => ['HashRef' => { -as => 'RealHash' }],
94              );
95
96           MooseX::Types (and experimentally, MouseX::Types) libraries can
97           also be added this way, but cannot be followed by an arrayref of
98           types to import.
99
100       "add_type($type, $name)"
101           The long-awaited singular form of "add_types". Given a type
102           constraint object, adds it to the registry with a given name. The
103           name may be omitted, in which case "$type->name" is called, and
104           Type::Registry will throw an error if $type is anonymous. If a name
105           is explicitly given, Type::Registry cares not one wit whether the
106           type constraint is anonymous.
107
108           This method can even add MooseX::Types and MouseX::Types type
109           constraints; indeed anything that can be handled by
110           Types::TypeTiny's "to_TypeTiny" function. (Bear in mind that
111           to_TypeTiny always results in an anonymous type constraint, so
112           $name will be required.)
113
114       "alias_type($oldname, $newname)"
115           Create an alias for an existing type.
116
117       simple_lookup($name)
118           Look up a type in the registry by name.
119
120           Returns undef if not found.
121
122       foreign_lookup($name)
123           Like "simple_lookup", but if the type name contains "::", will
124           attempt to load it from a type library. (And will attempt to load
125           that module.)
126
127       lookup($name)
128           Look up by name, with a DSL.
129
130              t->lookup("Int|ArrayRef[Int]")
131
132           The DSL can be summed up as:
133
134              X               type from this registry
135              My::Lib::X      type from a type library
136              ~X              complementary type
137              X | Y           union
138              X & Y           intersection
139              X[...]          parameterized type
140              slurpy X        slurpy type
141              Foo::Bar::      class type
142
143           Croaks if not found.
144
145       make_union(@constraints), make_intersection(@constraints),
146       make_class_type($class), make_role_type($role)
147           Convenience methods for creating certain common type constraints.
148
149       "AUTOLOAD"
150           Overloaded to call "lookup".
151
152              $registry->Str;  # like $registry->lookup("Str")
153
154       "get_parent", set_parent($reg), "clear_parent", "has_parent"
155           Advanced stuff. Allows a registry to have a "parent" registry which
156           it inherits type constraints from.
157
158   Functions
159       "t" This class can export a function "t" which acts like
160           ""Type::Registry"->for_class($importing_class)".
161

BUGS

163       Please report any bugs to
164       <https://github.com/tobyink/p5-type-tiny/issues>.
165

SEE ALSO

167       Type::Library.
168

AUTHOR

170       Toby Inkster <tobyink@cpan.org>.
171
173       This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
174
175       This is free software; you can redistribute it and/or modify it under
176       the same terms as the Perl 5 programming language system itself.
177

DISCLAIMER OF WARRANTIES

179       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
180       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
181       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
182
183
184
185perl v5.38.0                      2023-07-21                 Type::Registry(3)
Impressum