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(Types::Standard::HashRef, "new")
90 $classtype->plus_constructors() ## identical to above
91
92 This is handy for Moose/Mouse/Moo-based classes.
93
95 Please report any bugs to
96 <http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny>.
97
99 Type::Tiny::Manual.
100
101 Type::Tiny.
102
103 Moose::Meta::TypeConstraint::Class.
104
106 Toby Inkster <tobyink@cpan.org>.
107
109 This software is copyright (c) 2013-2014, 2017-2019 by Toby Inkster.
110
111 This is free software; you can redistribute it and/or modify it under
112 the same terms as the Perl 5 programming language system itself.
113
115 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
116 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
117 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
118
119
120
121perl v5.28.1 2019-01-08 Type::Tiny::Class(3)