1Type::Tiny::ConstrainedUOsbejrecCto(n3t)ributed Perl DocTuympeen:t:aTtiinoyn::ConstrainedObject(3)
2
3
4
6 Type::Tiny::ConstrainedObject - shared behavour for Type::Tiny::Class,
7 etc
8
10 This module is considered experiemental.
11
13 Methods
14 The following methods exist for Type::Tiny::Class, Type::Tiny::Role,
15 Type::Tiny::Duck, and any type constraints that inherit from "Object"
16 or "Overload" in Types::Standard.
17
18 These methods will also work for Type::Tiny::Intersection if at least
19 one of the types in the intersection provides these methods.
20
21 These methods will also work for Type::Tiny::Union if all of the types
22 in the union provide these methods.
23
24 "stringifies_to($constraint)"
25 Generates a new child type constraint which checks the object's
26 stringification against a constraint. For example:
27
28 my $type = Type::Tiny::Class->new(class => 'URI');
29 my $child = $type->stringifies_to( StrMatch[qr/^http:/] );
30
31 $child->assert_valid( URI->new("http://example.com/") );
32
33 In the above example, $child is a type constraint that checks
34 objects are blessed into (or inherit from) the URI class, and when
35 stringified (e.g. though overloading) the result matches the
36 regular expression "qr/^http:/".
37
38 $constraint may be a type constraint, something that can be coerced
39 to a type constraint (such as a coderef returning a boolean), a
40 string of Perl code operating on $_, or a reference to a regular
41 expression.
42
43 So the following would work:
44
45 my $child = $type->stringifies_to( sub { qr/^http:/ } );
46 my $child = $type->stringifies_to( qr/^http:/ );
47 my $child = $type->stringifies_to( 'm/^http:/' );
48
49 my $child = $type->where('"$_" =~ /^http:/');
50
51 "numifies_to($constraint)"
52 The same as "stringifies_to" but checks numification.
53
54 The following might be useful:
55
56 use Types::Standard qw(Int Overload);
57 my $IntLike = Int | Overload->numifies_to(Int)
58
59 "with_attribute_values($attr1 => $constraint1, ...)"
60 This is best explained with an example:
61
62 use Types::Standard qw(InstanceOf StrMatch);
63 use Types::Common::Numeric qw(IntRange);
64
65 my $person = InstanceOf['Local::Human'];
66 my $woman = $person->with_attribute_values(
67 gender => StrMatch[ qr/^F/i ],
68 age => IntRange[ 18 => () ],
69 );
70
71 $woman->assert_valid($alice);
72
73 This assertion will firstly check that $alice is a Local::Human,
74 then check that "$alice->gender" starts with an "F", and lastly
75 check that "$alice->age" is an integer at least 18.
76
77 Again, constraints can be type constraints, coderefs, strings of
78 Perl code, or regular expressions.
79
80 Technically the "attributes" don't need to be Moo/Moose/Mouse
81 attributes, but any methods which can be called with no parameters
82 and return a scalar.
83
85 Please report any bugs to
86 <https://github.com/tobyink/p5-type-tiny/issues>.
87
89 Type::Tiny::Manual.
90
91 Type::Tiny.
92
94 Toby Inkster <tobyink@cpan.org>.
95
97 This software is copyright (c) 2019-2021 by Toby Inkster.
98
99 This is free software; you can redistribute it and/or modify it under
100 the same terms as the Perl 5 programming language system itself.
101
103 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
104 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
105 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
106
107
108
109perl v5.34.0 2021-09-13 Type::Tiny::ConstrainedObject(3)