1Type::Tiny::Class(3) User Contributed Perl Documentation Type::Tiny::Class(3)
2
3
4
6 Type::Tiny::Class - type constraints based on the "isa" method
7
9 This module is covered by the Type-Tiny stability policy.
10
12 Type constraints of the general form "{ $_->isa("Some::Class") }".
13
14 This package inherits from Type::Tiny; see that for most documentation.
15 Major differences are listed below:
16
17 Constructor
18 "new"
19 When the constructor is called on an instance of Type::Tiny::Class,
20 it passes the call through to the constructor of the class for the
21 constraint. So for example:
22
23 my $type = Type::Tiny::Class->new(class => "Foo::Bar");
24 my $obj = $type->new(hello => "World");
25 say ref($obj); # prints "Foo::Bar"
26
27 This little bit of DWIM was borrowed from
28 MooseX::Types::TypeDecorator, but Type::Tiny doesn't take the idea
29 quite as far.
30
31 Attributes
32 "class"
33 The class for the constraint.
34
35 "constraint"
36 Unlike Type::Tiny, you cannot pass a constraint coderef to the
37 constructor. Instead rely on the default.
38
39 "inlined"
40 Unlike Type::Tiny, you cannot pass an inlining coderef to the
41 constructor. Instead rely on the default.
42
43 "parent"
44 Parent is automatically calculated, and cannot be passed to the
45 constructor.
46
47 Methods
48 "plus_constructors($source, $method_name)"
49 Much like "plus_coercions" but adds coercions that go via a
50 constructor. (In fact, this is implemented as a wrapper for
51 "plus_coercions".)
52
53 Example:
54
55 package MyApp::Minion;
56
57 use Moose; extends "MyApp::Person";
58
59 use Types::Standard qw( HashRef Str );
60 use Type::Utils qw( class_type );
61
62 my $Person = class_type({ class => "MyApp::Person" });
63
64 has boss => (
65 is => "ro",
66 isa => $Person->plus_constructors(
67 HashRef, "new",
68 Str, "_new_from_name",
69 ),
70 coerce => 1,
71 );
72
73 package main;
74
75 MyApp::Minion->new(
76 ...,
77 boss => "Bob", ## via MyApp::Person->_new_from_name
78 );
79
80 MyApp::Minion->new(
81 ...,
82 boss => { name => "Bob" }, ## via MyApp::Person->new
83 );
84
85 Because coercing "HashRef" via constructor is a common desire, if
86 you call "plus_constructors" with no arguments at all, this is the
87 default.
88
89 $classtype->plus_constructors(HashRef, "new")
90 $classtype->plus_constructors() ## identical to above
91
92 This is handy for Moose/Mouse/Moo-based classes.
93
94 "stringifies_to($constraint)"
95 See Type::Tiny::ConstrainedObject.
96
97 "numifies_to($constraint)"
98 See Type::Tiny::ConstrainedObject.
99
100 "with_attribute_values($attr1 => $constraint1, ...)"
101 See Type::Tiny::ConstrainedObject.
102
103 Exports
104 Type::Tiny::Class can be used as an exporter.
105
106 use Type::Tiny::Class 'HTTP::Tiny';
107
108 This will export the following functions into your namespace:
109
110 "HTTPTiny"
111 "is_HTTPTiny( $value )"
112 "assert_HTTPTiny( $value )"
113 "to_HTTPTiny( $value )"
114
115 You will also be able to use "HTTPTiny->new(...)" as a shortcut for
116 "HTTP::Tiny->new(...)".
117
118 Multiple types can be exported at once:
119
120 use Type::Tiny::Class qw( HTTP::Tiny LWP::UserAgent );
121
123 Please report any bugs to
124 <https://github.com/tobyink/p5-type-tiny/issues>.
125
127 Type::Tiny::Manual.
128
129 Type::Tiny.
130
131 Moose::Meta::TypeConstraint::Class.
132
134 Toby Inkster <tobyink@cpan.org>.
135
137 This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.
138
139 This is free software; you can redistribute it and/or modify it under
140 the same terms as the Perl 5 programming language system itself.
141
143 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
144 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
145 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
146
147
148
149perl v5.36.0 2023-01-04 Type::Tiny::Class(3)