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::Common qw( InstanceOf StrMatch IntRange );
63
64 my $person = InstanceOf['Local::Human'];
65 my $woman = $person->with_attribute_values(
66 gender => StrMatch[ qr/^F/i ],
67 age => IntRange[ 18 => () ],
68 );
69
70 $woman->assert_valid($alice);
71
72 This assertion will firstly check that $alice is a Local::Human,
73 then check that "$alice->gender" starts with an "F", and lastly
74 check that "$alice->age" is an integer at least 18.
75
76 Again, constraints can be type constraints, coderefs, strings of
77 Perl code, or regular expressions.
78
79 Technically the "attributes" don't need to be Moo/Moose/Mouse
80 attributes, but any methods which can be called with no parameters
81 and return a scalar.
82
84 Please report any bugs to
85 <https://github.com/tobyink/p5-type-tiny/issues>.
86
88 Type::Tiny::Manual.
89
90 Type::Tiny.
91
93 Toby Inkster <tobyink@cpan.org>.
94
96 This software is copyright (c) 2019-2023 by Toby Inkster.
97
98 This is free software; you can redistribute it and/or modify it under
99 the same terms as the Perl 5 programming language system itself.
100
102 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
103 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
104 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
105
106
107
108perl v5.36.0 2023-04-24 Type::Tiny::ConstrainedObject(3)