| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Moose::Util::TypeConstraints; |
|
2
|
|
|
|
|
|
|
our $VERSION = '2.2203'; |
|
3
|
|
|
|
|
|
|
|
|
4
|
401
|
|
|
401
|
|
3976195
|
use Carp (); |
|
|
401
|
|
|
|
|
1318
|
|
|
|
401
|
|
|
|
|
10662
|
|
|
5
|
401
|
|
|
401
|
|
1972
|
use Scalar::Util qw( blessed ); |
|
|
401
|
|
|
|
|
763
|
|
|
|
401
|
|
|
|
|
15776
|
|
|
6
|
401
|
|
|
401
|
|
22325
|
use Moose::Exporter; |
|
|
401
|
|
|
|
|
833
|
|
|
|
401
|
|
|
|
|
2681
|
|
|
7
|
401
|
|
|
401
|
|
2114
|
use Moose::Deprecated; |
|
|
401
|
|
|
|
|
881
|
|
|
|
401
|
|
|
|
|
2776
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
10
|
|
|
|
|
|
|
# Prototyped subs must be predeclared because we have a |
|
11
|
|
|
|
|
|
|
# circular dependency with Moose::Meta::Attribute et. al. |
|
12
|
|
|
|
|
|
|
# so in case of us being use'd first the predeclaration |
|
13
|
|
|
|
|
|
|
# ensures the prototypes are in scope when consumers are |
|
14
|
|
|
|
|
|
|
# compiled. |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# dah sugah! |
|
17
|
|
|
|
|
|
|
sub where (&); |
|
18
|
|
|
|
|
|
|
sub via (&); |
|
19
|
|
|
|
|
|
|
sub message (&); |
|
20
|
|
|
|
|
|
|
sub inline_as (&); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
23
|
|
|
|
|
|
|
|
|
24
|
401
|
|
|
401
|
|
46971
|
use Moose::Meta::TypeConstraint; |
|
|
401
|
|
|
|
|
963
|
|
|
|
401
|
|
|
|
|
11977
|
|
|
25
|
401
|
|
|
401
|
|
171050
|
use Moose::Meta::TypeConstraint::Union; |
|
|
401
|
|
|
|
|
1041
|
|
|
|
401
|
|
|
|
|
13009
|
|
|
26
|
401
|
|
|
401
|
|
169443
|
use Moose::Meta::TypeConstraint::Parameterized; |
|
|
401
|
|
|
|
|
1175
|
|
|
|
401
|
|
|
|
|
13449
|
|
|
27
|
401
|
|
|
401
|
|
3005
|
use Moose::Meta::TypeConstraint::Parameterizable; |
|
|
401
|
|
|
|
|
838
|
|
|
|
401
|
|
|
|
|
7828
|
|
|
28
|
401
|
|
|
401
|
|
174153
|
use Moose::Meta::TypeConstraint::Class; |
|
|
401
|
|
|
|
|
1056
|
|
|
|
401
|
|
|
|
|
13027
|
|
|
29
|
401
|
|
|
401
|
|
168291
|
use Moose::Meta::TypeConstraint::Role; |
|
|
401
|
|
|
|
|
1021
|
|
|
|
401
|
|
|
|
|
12642
|
|
|
30
|
401
|
|
|
401
|
|
167757
|
use Moose::Meta::TypeConstraint::Enum; |
|
|
401
|
|
|
|
|
1131
|
|
|
|
401
|
|
|
|
|
13466
|
|
|
31
|
401
|
|
|
401
|
|
167197
|
use Moose::Meta::TypeConstraint::DuckType; |
|
|
401
|
|
|
|
|
1065
|
|
|
|
401
|
|
|
|
|
12826
|
|
|
32
|
401
|
|
|
401
|
|
2614
|
use Moose::Meta::TypeCoercion; |
|
|
401
|
|
|
|
|
823
|
|
|
|
401
|
|
|
|
|
8324
|
|
|
33
|
401
|
|
|
401
|
|
1961
|
use Moose::Meta::TypeCoercion::Union; |
|
|
401
|
|
|
|
|
775
|
|
|
|
401
|
|
|
|
|
7045
|
|
|
34
|
401
|
|
|
401
|
|
169750
|
use Moose::Meta::TypeConstraint::Registry; |
|
|
401
|
|
|
|
|
1340
|
|
|
|
401
|
|
|
|
|
14288
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
401
|
|
|
401
|
|
2750
|
use Moose::Util 'throw_exception'; |
|
|
401
|
|
|
|
|
872
|
|
|
|
401
|
|
|
|
|
1786
|
|
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
|
39
|
|
|
|
|
|
|
as_is => [ |
|
40
|
|
|
|
|
|
|
qw( |
|
41
|
|
|
|
|
|
|
type subtype class_type role_type maybe_type duck_type |
|
42
|
|
|
|
|
|
|
as where message inline_as |
|
43
|
|
|
|
|
|
|
coerce from via |
|
44
|
|
|
|
|
|
|
enum union |
|
45
|
|
|
|
|
|
|
find_type_constraint |
|
46
|
|
|
|
|
|
|
register_type_constraint |
|
47
|
|
|
|
|
|
|
match_on_type ) |
|
48
|
|
|
|
|
|
|
], |
|
49
|
|
|
|
|
|
|
); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
52
|
|
|
|
|
|
|
## type registry and some useful functions for it |
|
53
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new; |
|
56
|
|
|
|
|
|
|
|
|
57
|
4
|
|
|
4
|
1
|
664
|
sub get_type_constraint_registry {$REGISTRY} |
|
58
|
401
|
|
|
401
|
1
|
904
|
sub list_all_type_constraints { keys %{ $REGISTRY->type_constraints } } |
|
|
401
|
|
|
|
|
11313
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub export_type_constraints_as_functions { |
|
61
|
5
|
|
|
5
|
1
|
495
|
my $pkg = caller(); |
|
62
|
401
|
|
|
401
|
|
128022
|
no strict 'refs'; |
|
|
401
|
|
|
|
|
1009
|
|
|
|
401
|
|
|
|
|
1120376
|
|
|
63
|
5
|
|
|
|
|
13
|
foreach my $constraint ( keys %{ $REGISTRY->type_constraints } ) { |
|
|
5
|
|
|
|
|
161
|
|
|
64
|
120
|
|
|
|
|
268
|
my $tc = $REGISTRY->get_type_constraint($constraint) |
|
65
|
|
|
|
|
|
|
->_compiled_type_constraint; |
|
66
|
120
|
|
|
|
|
567
|
*{"${pkg}::${constraint}"} |
|
67
|
120
|
100
|
|
141
|
|
360
|
= sub { $tc->( $_[0] ) ? 1 : undef }; # the undef is for compat |
|
|
141
|
|
|
|
|
34950
|
|
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub create_type_constraint_union { |
|
72
|
66
|
|
|
66
|
1
|
602
|
_create_type_constraint_union(\@_); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub create_named_type_constraint_union { |
|
76
|
3
|
|
|
3
|
1
|
7
|
my $name = shift; |
|
77
|
3
|
|
|
|
|
13
|
_create_type_constraint_union($name, \@_); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _create_type_constraint_union { |
|
81
|
69
|
|
|
69
|
|
136
|
my $name; |
|
82
|
69
|
100
|
|
|
|
245
|
$name = shift if @_ > 1; |
|
83
|
69
|
|
|
|
|
143
|
my @tcs = @{ shift() }; |
|
|
69
|
|
|
|
|
189
|
|
|
84
|
|
|
|
|
|
|
|
|
85
|
69
|
|
|
|
|
152
|
my @type_constraint_names; |
|
86
|
|
|
|
|
|
|
|
|
87
|
69
|
100
|
66
|
|
|
338
|
if ( scalar @tcs == 1 && _detect_type_constraint_union( $tcs[0] ) ) { |
|
88
|
62
|
|
|
|
|
252
|
@type_constraint_names = _parse_type_constraint_union( $tcs[0] ); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
else { |
|
91
|
7
|
|
|
|
|
28
|
@type_constraint_names = @tcs; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
68
|
100
|
|
|
|
298
|
( scalar @type_constraint_names >= 2 ) |
|
95
|
|
|
|
|
|
|
|| throw_exception("UnionTakesAtleastTwoTypeNames"); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my @type_constraints = map { |
|
98
|
66
|
100
|
|
|
|
180
|
find_or_parse_type_constraint($_) |
|
|
134
|
|
|
|
|
313
|
|
|
99
|
|
|
|
|
|
|
|| throw_exception( CouldNotLocateTypeConstraintForUnion => type_name => $_ ); |
|
100
|
|
|
|
|
|
|
} @type_constraint_names; |
|
101
|
|
|
|
|
|
|
|
|
102
|
65
|
|
|
|
|
262
|
my %options = ( |
|
103
|
|
|
|
|
|
|
type_constraints => \@type_constraints |
|
104
|
|
|
|
|
|
|
); |
|
105
|
65
|
100
|
|
|
|
203
|
$options{name} = $name if defined $name; |
|
106
|
|
|
|
|
|
|
|
|
107
|
65
|
|
|
|
|
888
|
return Moose::Meta::TypeConstraint::Union->new(%options); |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub create_parameterized_type_constraint { |
|
112
|
134
|
|
|
134
|
1
|
490
|
my $type_constraint_name = shift; |
|
113
|
134
|
|
|
|
|
422
|
my ( $base_type, $type_parameter ) |
|
114
|
|
|
|
|
|
|
= _parse_parameterized_type_constraint($type_constraint_name); |
|
115
|
|
|
|
|
|
|
|
|
116
|
134
|
100
|
66
|
|
|
840
|
( defined $base_type && defined $type_parameter ) |
|
117
|
|
|
|
|
|
|
|| throw_exception( InvalidTypeGivenToCreateParameterizedTypeConstraint => type_name => $type_constraint_name ); |
|
118
|
|
|
|
|
|
|
|
|
119
|
133
|
100
|
|
|
|
499
|
if ( $REGISTRY->has_type_constraint($base_type) ) { |
|
120
|
132
|
|
|
|
|
630
|
my $base_type_tc = $REGISTRY->get_type_constraint($base_type); |
|
121
|
132
|
|
|
|
|
704
|
return _create_parameterized_type_constraint( |
|
122
|
|
|
|
|
|
|
$base_type_tc, |
|
123
|
|
|
|
|
|
|
$type_parameter |
|
124
|
|
|
|
|
|
|
); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
else { |
|
127
|
1
|
|
|
|
|
5
|
throw_exception( InvalidBaseTypeGivenToCreateParameterizedTypeConstraint => type_name => $base_type ); |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _create_parameterized_type_constraint { |
|
132
|
132
|
|
|
132
|
|
699
|
my ( $base_type_tc, $type_parameter ) = @_; |
|
133
|
132
|
100
|
|
|
|
1143
|
if ( $base_type_tc->can('parameterize') ) { |
|
134
|
129
|
|
|
|
|
570
|
return $base_type_tc->parameterize($type_parameter); |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
else { |
|
137
|
3
|
|
|
|
|
71
|
return Moose::Meta::TypeConstraint::Parameterized->new( |
|
138
|
|
|
|
|
|
|
name => $base_type_tc->name . '[' . $type_parameter . ']', |
|
139
|
|
|
|
|
|
|
parent => $base_type_tc, |
|
140
|
|
|
|
|
|
|
type_parameter => |
|
141
|
|
|
|
|
|
|
find_or_create_isa_type_constraint($type_parameter), |
|
142
|
|
|
|
|
|
|
); |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
#should we also support optimized checks? |
|
147
|
|
|
|
|
|
|
sub create_class_type_constraint { |
|
148
|
1793
|
|
|
1793
|
1
|
4739
|
my ( $class, $options ) = @_; |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# too early for this check |
|
151
|
|
|
|
|
|
|
#find_type_constraint("ClassName")->check($class) |
|
152
|
|
|
|
|
|
|
# || __PACKAGE__->_throw_error("Can't create a class type constraint because '$class' is not a class name"); |
|
153
|
|
|
|
|
|
|
|
|
154
|
1793
|
|
100
|
|
|
8633
|
my $pkg_defined_in = $options->{package_defined_in} || scalar( caller(1) ); |
|
155
|
|
|
|
|
|
|
|
|
156
|
1793
|
100
|
|
|
|
5841
|
if (my $type = $REGISTRY->get_type_constraint($class)) { |
|
157
|
17
|
100
|
66
|
|
|
463
|
if (!($type->isa('Moose::Meta::TypeConstraint::Class') && $type->class eq $class)) { |
|
158
|
6
|
|
|
|
|
172
|
throw_exception( TypeConstraintIsAlreadyCreated => package_defined_in => $pkg_defined_in, |
|
159
|
|
|
|
|
|
|
type_name => $type->name, |
|
160
|
|
|
|
|
|
|
); |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
else { |
|
163
|
11
|
|
|
|
|
57
|
return $type; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my %options = ( |
|
168
|
|
|
|
|
|
|
class => $class, |
|
169
|
|
|
|
|
|
|
name => $class, |
|
170
|
|
|
|
|
|
|
package_defined_in => $pkg_defined_in, |
|
171
|
1776
|
50
|
|
|
|
4037
|
%{ $options || {} }, # overrides options from above |
|
|
1776
|
|
|
|
|
9850
|
|
|
172
|
|
|
|
|
|
|
); |
|
173
|
|
|
|
|
|
|
|
|
174
|
1776
|
|
50
|
|
|
5588
|
$options{name} ||= "__ANON__"; |
|
175
|
|
|
|
|
|
|
|
|
176
|
1776
|
|
|
|
|
14921
|
my $tc = Moose::Meta::TypeConstraint::Class->new(%options); |
|
177
|
1776
|
|
|
|
|
8506
|
$REGISTRY->add_type_constraint($tc); |
|
178
|
1776
|
|
|
|
|
7819
|
return $tc; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub create_role_type_constraint { |
|
182
|
1306
|
|
|
1306
|
1
|
3960
|
my ( $role, $options ) = @_; |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# too early for this check |
|
185
|
|
|
|
|
|
|
#find_type_constraint("ClassName")->check($class) |
|
186
|
|
|
|
|
|
|
# || __PACKAGE__->_throw_error("Can't create a class type constraint because '$class' is not a class name"); |
|
187
|
|
|
|
|
|
|
|
|
188
|
1306
|
|
100
|
|
|
7057
|
my $pkg_defined_in = $options->{package_defined_in} || scalar( caller(1) ); |
|
189
|
|
|
|
|
|
|
|
|
190
|
1306
|
100
|
|
|
|
4613
|
if (my $type = $REGISTRY->get_type_constraint($role)) { |
|
191
|
11
|
100
|
66
|
|
|
242
|
if (!($type->isa('Moose::Meta::TypeConstraint::Role') && $type->role eq $role)) { |
|
192
|
6
|
|
|
|
|
175
|
throw_exception( TypeConstraintIsAlreadyCreated => type_name => $type->name, |
|
193
|
|
|
|
|
|
|
package_defined_in => $pkg_defined_in |
|
194
|
|
|
|
|
|
|
); |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
else { |
|
197
|
5
|
|
|
|
|
27
|
return $type; |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my %options = ( |
|
202
|
|
|
|
|
|
|
role => $role, |
|
203
|
|
|
|
|
|
|
name => $role, |
|
204
|
|
|
|
|
|
|
package_defined_in => $pkg_defined_in, |
|
205
|
1295
|
50
|
|
|
|
2923
|
%{ $options || {} }, |
|
|
1295
|
|
|
|
|
7275
|
|
|
206
|
|
|
|
|
|
|
); |
|
207
|
|
|
|
|
|
|
|
|
208
|
1295
|
|
100
|
|
|
4091
|
$options{name} ||= "__ANON__"; |
|
209
|
|
|
|
|
|
|
|
|
210
|
1295
|
|
|
|
|
10915
|
my $tc = Moose::Meta::TypeConstraint::Role->new(%options); |
|
211
|
1295
|
|
|
|
|
6611
|
$REGISTRY->add_type_constraint($tc); |
|
212
|
1295
|
|
|
|
|
5921
|
return $tc; |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub find_or_create_type_constraint { |
|
216
|
216
|
|
|
216
|
0
|
7865
|
my ( $type_constraint_name, $options_for_anon_type ) = @_; |
|
217
|
|
|
|
|
|
|
|
|
218
|
216
|
50
|
|
|
|
698
|
if ( my $constraint |
|
|
|
0
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
= find_or_parse_type_constraint($type_constraint_name) ) { |
|
220
|
216
|
|
|
|
|
757
|
return $constraint; |
|
221
|
|
|
|
|
|
|
} |
|
222
|
|
|
|
|
|
|
elsif ( defined $options_for_anon_type ) { |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# NOTE: |
|
225
|
|
|
|
|
|
|
# if there is no $options_for_anon_type |
|
226
|
|
|
|
|
|
|
# specified, then we assume they don't |
|
227
|
|
|
|
|
|
|
# want to create one, and return nothing. |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# otherwise assume that we should create |
|
230
|
|
|
|
|
|
|
# an ANON type with the $options_for_anon_type |
|
231
|
|
|
|
|
|
|
# options which can be passed in. It should |
|
232
|
|
|
|
|
|
|
# be noted that these don't get registered |
|
233
|
|
|
|
|
|
|
# so we need to return it. |
|
234
|
|
|
|
|
|
|
# - SL |
|
235
|
|
|
|
|
|
|
return Moose::Meta::TypeConstraint->new( |
|
236
|
|
|
|
|
|
|
name => '__ANON__', |
|
237
|
0
|
|
|
|
|
0
|
%{$options_for_anon_type} |
|
|
0
|
|
|
|
|
0
|
|
|
238
|
|
|
|
|
|
|
); |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
0
|
return; |
|
242
|
|
|
|
|
|
|
} |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub find_or_create_isa_type_constraint { |
|
245
|
8621
|
|
|
8621
|
1
|
16693
|
my ($type_constraint_name, $options) = @_; |
|
246
|
8621
|
100
|
|
|
|
17341
|
find_or_parse_type_constraint($type_constraint_name) |
|
247
|
|
|
|
|
|
|
|| create_class_type_constraint($type_constraint_name, $options); |
|
248
|
|
|
|
|
|
|
} |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
sub find_or_create_does_type_constraint { |
|
251
|
17
|
|
|
17
|
1
|
51
|
my ($type_constraint_name, $options) = @_; |
|
252
|
17
|
100
|
|
|
|
64
|
find_or_parse_type_constraint($type_constraint_name) |
|
253
|
|
|
|
|
|
|
|| create_role_type_constraint($type_constraint_name, $options); |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub find_or_parse_type_constraint { |
|
257
|
9460
|
|
|
9460
|
1
|
20551
|
my $type_constraint_name = normalize_type_constraint_name(shift); |
|
258
|
9460
|
|
|
|
|
13784
|
my $constraint; |
|
259
|
|
|
|
|
|
|
|
|
260
|
9460
|
100
|
|
|
|
17548
|
if ( $constraint = find_type_constraint($type_constraint_name) ) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
261
|
8989
|
|
|
|
|
21231
|
return $constraint; |
|
262
|
|
|
|
|
|
|
} |
|
263
|
|
|
|
|
|
|
elsif ( _detect_type_constraint_union($type_constraint_name) ) { |
|
264
|
57
|
|
|
|
|
247
|
$constraint = create_type_constraint_union($type_constraint_name); |
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
elsif ( _detect_parameterized_type_constraint($type_constraint_name) ) { |
|
267
|
127
|
|
|
|
|
729
|
$constraint |
|
268
|
|
|
|
|
|
|
= create_parameterized_type_constraint($type_constraint_name); |
|
269
|
|
|
|
|
|
|
} |
|
270
|
|
|
|
|
|
|
else { |
|
271
|
287
|
|
|
|
|
2559
|
return; |
|
272
|
|
|
|
|
|
|
} |
|
273
|
|
|
|
|
|
|
|
|
274
|
182
|
|
|
|
|
943
|
$REGISTRY->add_type_constraint($constraint); |
|
275
|
182
|
|
|
|
|
624
|
return $constraint; |
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub normalize_type_constraint_name { |
|
279
|
9460
|
|
|
9460
|
1
|
13741
|
my $type_constraint_name = shift; |
|
280
|
9460
|
|
|
|
|
19558
|
$type_constraint_name =~ s/\s//g; |
|
281
|
9460
|
|
|
|
|
15876
|
return $type_constraint_name; |
|
282
|
|
|
|
|
|
|
} |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
sub _confess { |
|
285
|
0
|
|
|
0
|
|
0
|
my $error = shift; |
|
286
|
|
|
|
|
|
|
|
|
287
|
0
|
|
|
|
|
0
|
local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
|
288
|
0
|
|
|
|
|
0
|
Carp::confess($error); |
|
289
|
|
|
|
|
|
|
} |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
292
|
|
|
|
|
|
|
## exported functions ... |
|
293
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub find_type_constraint { |
|
296
|
20569
|
|
|
20569
|
1
|
87029
|
my $type = shift; |
|
297
|
|
|
|
|
|
|
|
|
298
|
20569
|
100
|
66
|
|
|
63828
|
if ( blessed $type and $type->isa("Moose::Meta::TypeConstraint") ) { |
|
299
|
2192
|
|
|
|
|
5703
|
return $type; |
|
300
|
|
|
|
|
|
|
} |
|
301
|
|
|
|
|
|
|
else { |
|
302
|
18377
|
100
|
|
|
|
48837
|
return unless $REGISTRY->has_type_constraint($type); |
|
303
|
15104
|
|
|
|
|
37453
|
return $REGISTRY->get_type_constraint($type); |
|
304
|
|
|
|
|
|
|
} |
|
305
|
|
|
|
|
|
|
} |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
sub register_type_constraint { |
|
308
|
45
|
|
|
45
|
1
|
159
|
my $constraint = shift; |
|
309
|
45
|
100
|
|
|
|
1125
|
throw_exception( CannotRegisterUnnamedTypeConstraint => type => $constraint ) |
|
310
|
|
|
|
|
|
|
unless defined $constraint->name; |
|
311
|
44
|
|
|
|
|
231
|
$REGISTRY->add_type_constraint($constraint); |
|
312
|
44
|
|
|
|
|
208
|
return $constraint; |
|
313
|
|
|
|
|
|
|
} |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
# type constructors |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
sub type { |
|
318
|
424
|
|
|
424
|
1
|
2071
|
my $name = shift; |
|
319
|
|
|
|
|
|
|
|
|
320
|
424
|
|
|
|
|
1263
|
my %p = map { %{$_} } @_; |
|
|
817
|
|
|
|
|
1312
|
|
|
|
817
|
|
|
|
|
3385
|
|
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
return _create_type_constraint( |
|
323
|
|
|
|
|
|
|
$name, undef, $p{where}, $p{message}, |
|
324
|
|
|
|
|
|
|
$p{inline_as}, |
|
325
|
424
|
|
|
|
|
2823
|
); |
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
sub subtype { |
|
329
|
6549
|
100
|
100
|
6549
|
1
|
19065
|
if ( @_ == 1 && !ref $_[0] ) { |
|
330
|
2
|
|
|
|
|
10
|
throw_exception( NoParentGivenToSubtype => name => $_[0] ); |
|
331
|
|
|
|
|
|
|
} |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
# The blessed check is mostly to accommodate MooseX::Types, which |
|
334
|
|
|
|
|
|
|
# uses an object which overloads stringification as a type name. |
|
335
|
6547
|
100
|
66
|
|
|
18368
|
my $name = ref $_[0] && !blessed $_[0] ? undef : shift; |
|
336
|
|
|
|
|
|
|
|
|
337
|
6547
|
|
|
|
|
12311
|
my %p = map { %{$_} } @_; |
|
|
19053
|
|
|
|
|
22123
|
|
|
|
19053
|
|
|
|
|
51629
|
|
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
# subtype Str => where { ... }; |
|
340
|
6547
|
100
|
|
|
|
17093
|
if ( !exists $p{as} ) { |
|
341
|
6
|
|
|
|
|
16
|
$p{as} = $name; |
|
342
|
6
|
|
|
|
|
13
|
$name = undef; |
|
343
|
|
|
|
|
|
|
} |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
return _create_type_constraint( |
|
346
|
|
|
|
|
|
|
$name, $p{as}, $p{where}, $p{message}, |
|
347
|
|
|
|
|
|
|
$p{inline_as}, |
|
348
|
6547
|
|
|
|
|
20373
|
); |
|
349
|
|
|
|
|
|
|
} |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
sub class_type { |
|
352
|
1515
|
|
|
1515
|
1
|
9231
|
create_class_type_constraint(@_); |
|
353
|
|
|
|
|
|
|
} |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub role_type ($;$) { |
|
356
|
1301
|
|
|
1301
|
1
|
5322
|
create_role_type_constraint(@_); |
|
357
|
|
|
|
|
|
|
} |
|
358
|
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
sub maybe_type { |
|
360
|
1
|
|
|
1
|
1
|
3
|
my ($type_parameter) = @_; |
|
361
|
|
|
|
|
|
|
|
|
362
|
1
|
|
|
|
|
4
|
register_type_constraint( |
|
363
|
|
|
|
|
|
|
$REGISTRY->get_type_constraint('Maybe')->parameterize($type_parameter) |
|
364
|
|
|
|
|
|
|
); |
|
365
|
|
|
|
|
|
|
} |
|
366
|
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
sub duck_type { |
|
368
|
25
|
|
|
25
|
1
|
1844
|
my ( $type_name, @methods ) = @_; |
|
369
|
25
|
100
|
66
|
|
|
141
|
if ( ref $type_name eq 'ARRAY' && !@methods ) { |
|
370
|
1
|
|
|
|
|
3
|
@methods = ($type_name); |
|
371
|
1
|
|
|
|
|
4
|
$type_name = undef; |
|
372
|
|
|
|
|
|
|
} |
|
373
|
25
|
100
|
66
|
|
|
182
|
if ( @methods == 1 && ref $methods[0] eq 'ARRAY' ) { |
|
374
|
24
|
|
|
|
|
61
|
@methods = @{ $methods[0] }; |
|
|
24
|
|
|
|
|
74
|
|
|
375
|
|
|
|
|
|
|
} |
|
376
|
|
|
|
|
|
|
else { |
|
377
|
1
|
|
|
|
|
4
|
Moose::Deprecated::deprecated( |
|
378
|
|
|
|
|
|
|
feature => 'non-arrayref form of duck_type', |
|
379
|
|
|
|
|
|
|
message => "Passing a list of values to duck_type is deprecated. " |
|
380
|
|
|
|
|
|
|
. "The method names should be wrapped in an arrayref.", |
|
381
|
|
|
|
|
|
|
); |
|
382
|
|
|
|
|
|
|
} |
|
383
|
|
|
|
|
|
|
|
|
384
|
25
|
|
|
|
|
271
|
register_type_constraint( |
|
385
|
|
|
|
|
|
|
create_duck_type_constraint( |
|
386
|
|
|
|
|
|
|
$type_name, |
|
387
|
|
|
|
|
|
|
\@methods, |
|
388
|
|
|
|
|
|
|
) |
|
389
|
|
|
|
|
|
|
); |
|
390
|
|
|
|
|
|
|
} |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
sub coerce { |
|
393
|
44
|
|
|
44
|
1
|
203
|
my ( $type_name, @coercion_map ) = @_; |
|
394
|
44
|
|
|
|
|
189
|
_install_type_coercions( $type_name, \@coercion_map ); |
|
395
|
|
|
|
|
|
|
} |
|
396
|
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
# The trick of returning @_ lets us avoid having to specify a |
|
398
|
|
|
|
|
|
|
# prototype. Perl will parse this: |
|
399
|
|
|
|
|
|
|
# |
|
400
|
|
|
|
|
|
|
# subtype 'Foo' |
|
401
|
|
|
|
|
|
|
# => as 'Str' |
|
402
|
|
|
|
|
|
|
# => where { ... } |
|
403
|
|
|
|
|
|
|
# |
|
404
|
|
|
|
|
|
|
# as this: |
|
405
|
|
|
|
|
|
|
# |
|
406
|
|
|
|
|
|
|
# subtype( 'Foo', as( 'Str', where { ... } ) ); |
|
407
|
|
|
|
|
|
|
# |
|
408
|
|
|
|
|
|
|
# If as() returns all its extra arguments, this just works, and |
|
409
|
|
|
|
|
|
|
# preserves backwards compatibility. |
|
410
|
6537
|
|
|
6537
|
1
|
34622
|
sub as { +{ as => shift }, @_ } |
|
411
|
6502
|
|
|
6502
|
1
|
66638
|
sub where (&) { +{ where => $_[0] } } |
|
412
|
12
|
|
|
12
|
1
|
1741
|
sub message (&) { +{ message => $_[0] } } |
|
413
|
6818
|
|
|
6818
|
1
|
21846
|
sub inline_as (&) { +{ inline_as => $_[0] } } |
|
414
|
|
|
|
|
|
|
|
|
415
|
47
|
|
|
47
|
1
|
292
|
sub from { @_ } |
|
416
|
46
|
|
|
46
|
1
|
21993
|
sub via (&) { $_[0] } |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
sub enum { |
|
419
|
16
|
|
|
16
|
1
|
2979
|
my ( $type_name, @values ) = @_; |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
# NOTE: |
|
422
|
|
|
|
|
|
|
# if only an array-ref is passed then |
|
423
|
|
|
|
|
|
|
# you get an anon-enum |
|
424
|
|
|
|
|
|
|
# - SL |
|
425
|
16
|
100
|
|
|
|
71
|
if ( ref $type_name eq 'ARRAY' ) { |
|
426
|
7
|
100
|
|
|
|
38
|
@values == 0 |
|
427
|
|
|
|
|
|
|
|| throw_exception( EnumCalledWithAnArrayRefAndAdditionalArgs => array => $type_name, |
|
428
|
|
|
|
|
|
|
args => \@values |
|
429
|
|
|
|
|
|
|
); |
|
430
|
5
|
|
|
|
|
17
|
@values = ($type_name); |
|
431
|
5
|
|
|
|
|
13
|
$type_name = undef; |
|
432
|
|
|
|
|
|
|
} |
|
433
|
14
|
100
|
66
|
|
|
90
|
if ( @values == 1 && ref $values[0] eq 'ARRAY' ) { |
|
434
|
13
|
|
|
|
|
28
|
@values = @{ $values[0] }; |
|
|
13
|
|
|
|
|
50
|
|
|
435
|
|
|
|
|
|
|
} |
|
436
|
|
|
|
|
|
|
else { |
|
437
|
1
|
|
|
|
|
7
|
Moose::Deprecated::deprecated( |
|
438
|
|
|
|
|
|
|
feature => 'non-arrayref form of enum', |
|
439
|
|
|
|
|
|
|
message => "Passing a list of values to enum is deprecated. " |
|
440
|
|
|
|
|
|
|
. "Enum values should be wrapped in an arrayref.", |
|
441
|
|
|
|
|
|
|
); |
|
442
|
|
|
|
|
|
|
} |
|
443
|
|
|
|
|
|
|
|
|
444
|
14
|
|
|
|
|
302
|
register_type_constraint( |
|
445
|
|
|
|
|
|
|
create_enum_type_constraint( |
|
446
|
|
|
|
|
|
|
$type_name, |
|
447
|
|
|
|
|
|
|
\@values, |
|
448
|
|
|
|
|
|
|
) |
|
449
|
|
|
|
|
|
|
); |
|
450
|
|
|
|
|
|
|
} |
|
451
|
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
sub union { |
|
453
|
6
|
|
|
6
|
1
|
1665
|
my ( $type_name, @constraints ) = @_; |
|
454
|
6
|
100
|
|
|
|
30
|
if ( ref $type_name eq 'ARRAY' ) { |
|
455
|
3
|
100
|
|
|
|
16
|
@constraints == 0 |
|
456
|
|
|
|
|
|
|
|| throw_exception( UnionCalledWithAnArrayRefAndAdditionalArgs => array => $type_name, |
|
457
|
|
|
|
|
|
|
args => \@constraints |
|
458
|
|
|
|
|
|
|
); |
|
459
|
2
|
|
|
|
|
6
|
@constraints = @$type_name; |
|
460
|
2
|
|
|
|
|
6
|
$type_name = undef; |
|
461
|
|
|
|
|
|
|
} |
|
462
|
5
|
100
|
100
|
|
|
32
|
if ( @constraints == 1 && ref $constraints[0] eq 'ARRAY' ) { |
|
463
|
2
|
|
|
|
|
5
|
@constraints = @{ $constraints[0] }; |
|
|
2
|
|
|
|
|
5
|
|
|
464
|
|
|
|
|
|
|
} |
|
465
|
5
|
100
|
|
|
|
17
|
if ( defined $type_name ) { |
|
466
|
3
|
|
|
|
|
13
|
return register_type_constraint( |
|
467
|
|
|
|
|
|
|
create_named_type_constraint_union( $type_name, @constraints ) |
|
468
|
|
|
|
|
|
|
); |
|
469
|
|
|
|
|
|
|
} |
|
470
|
2
|
|
|
|
|
8
|
return create_type_constraint_union( @constraints ); |
|
471
|
|
|
|
|
|
|
} |
|
472
|
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
sub create_enum_type_constraint { |
|
474
|
14
|
|
|
14
|
1
|
63
|
my ( $type_name, $values ) = @_; |
|
475
|
|
|
|
|
|
|
|
|
476
|
14
|
|
100
|
|
|
173
|
Moose::Meta::TypeConstraint::Enum->new( |
|
477
|
|
|
|
|
|
|
name => $type_name || '__ANON__', |
|
478
|
|
|
|
|
|
|
values => $values, |
|
479
|
|
|
|
|
|
|
); |
|
480
|
|
|
|
|
|
|
} |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
sub create_duck_type_constraint { |
|
483
|
25
|
|
|
25
|
1
|
71
|
my ( $type_name, $methods ) = @_; |
|
484
|
|
|
|
|
|
|
|
|
485
|
25
|
|
100
|
|
|
285
|
Moose::Meta::TypeConstraint::DuckType->new( |
|
486
|
|
|
|
|
|
|
name => $type_name || '__ANON__', |
|
487
|
|
|
|
|
|
|
methods => $methods, |
|
488
|
|
|
|
|
|
|
); |
|
489
|
|
|
|
|
|
|
} |
|
490
|
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
sub match_on_type { |
|
492
|
187
|
|
|
187
|
1
|
15911
|
my ($to_match, @cases) = @_; |
|
493
|
187
|
|
|
|
|
281
|
my $default; |
|
494
|
187
|
100
|
|
|
|
454
|
if (@cases % 2 != 0) { |
|
495
|
38
|
|
|
|
|
67
|
$default = pop @cases; |
|
496
|
38
|
100
|
|
|
|
116
|
(ref $default eq 'CODE') |
|
497
|
|
|
|
|
|
|
|| throw_exception( DefaultToMatchOnTypeMustBeCodeRef => to_match => $to_match, |
|
498
|
|
|
|
|
|
|
default_action => $default, |
|
499
|
|
|
|
|
|
|
cases_to_be_matched => \@cases |
|
500
|
|
|
|
|
|
|
); |
|
501
|
|
|
|
|
|
|
} |
|
502
|
186
|
|
|
|
|
385
|
while (@cases) { |
|
503
|
421
|
|
|
|
|
2728
|
my ($type, $action) = splice @cases, 0, 2; |
|
504
|
|
|
|
|
|
|
|
|
505
|
421
|
100
|
66
|
|
|
1295
|
unless (blessed $type && $type->isa('Moose::Meta::TypeConstraint')) { |
|
506
|
411
|
|
66
|
|
|
744
|
$type = find_or_parse_type_constraint($type) |
|
507
|
|
|
|
|
|
|
|| throw_exception( CannotFindTypeGivenToMatchOnType => type => $type, |
|
508
|
|
|
|
|
|
|
to_match => $to_match, |
|
509
|
|
|
|
|
|
|
action => $action |
|
510
|
|
|
|
|
|
|
); |
|
511
|
|
|
|
|
|
|
} |
|
512
|
|
|
|
|
|
|
|
|
513
|
420
|
100
|
|
|
|
1076
|
(ref $action eq 'CODE') |
|
514
|
|
|
|
|
|
|
|| throw_exception( MatchActionMustBeACodeRef => type_name => $type->name, |
|
515
|
|
|
|
|
|
|
action => $action, |
|
516
|
|
|
|
|
|
|
to_match => $to_match |
|
517
|
|
|
|
|
|
|
); |
|
518
|
|
|
|
|
|
|
|
|
519
|
419
|
100
|
|
|
|
988
|
if ($type->check($to_match)) { |
|
520
|
180
|
|
|
|
|
1518
|
local $_ = $to_match; |
|
521
|
180
|
|
|
|
|
429
|
return $action->($to_match); |
|
522
|
|
|
|
|
|
|
} |
|
523
|
|
|
|
|
|
|
} |
|
524
|
4
|
100
|
|
|
|
71
|
(defined $default) |
|
525
|
|
|
|
|
|
|
|| throw_exception( NoCasesMatched => to_match => $to_match, |
|
526
|
|
|
|
|
|
|
cases_to_be_matched => \@cases |
|
527
|
|
|
|
|
|
|
); |
|
528
|
|
|
|
|
|
|
{ |
|
529
|
2
|
|
|
|
|
4
|
local $_ = $to_match; |
|
|
2
|
|
|
|
|
6
|
|
|
530
|
2
|
|
|
|
|
10
|
return $default->($to_match); |
|
531
|
|
|
|
|
|
|
} |
|
532
|
|
|
|
|
|
|
} |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
536
|
|
|
|
|
|
|
## desugaring functions ... |
|
537
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
sub _create_type_constraint ($$$;$) { |
|
540
|
6971
|
|
|
6971
|
|
10368
|
my $name = shift; |
|
541
|
6971
|
|
|
|
|
8799
|
my $parent = shift; |
|
542
|
6971
|
|
|
|
|
9188
|
my $check = shift; |
|
543
|
6971
|
|
|
|
|
13331
|
my $message = shift; |
|
544
|
6971
|
|
|
|
|
8992
|
my $inlined = shift; |
|
545
|
|
|
|
|
|
|
|
|
546
|
6971
|
|
|
|
|
13184
|
my $pkg_defined_in = scalar( caller(1) ); |
|
547
|
|
|
|
|
|
|
|
|
548
|
6971
|
100
|
|
|
|
13477
|
if ( defined $name ) { |
|
549
|
6949
|
|
|
|
|
18023
|
my $type = $REGISTRY->get_type_constraint($name); |
|
550
|
|
|
|
|
|
|
|
|
551
|
6949
|
100
|
66
|
|
|
15812
|
( $type->_package_defined_in eq $pkg_defined_in ) |
|
552
|
|
|
|
|
|
|
|| throw_exception( TypeConstraintIsAlreadyCreated => package_defined_in => $pkg_defined_in, |
|
553
|
|
|
|
|
|
|
type_name => $type->name, |
|
554
|
|
|
|
|
|
|
) |
|
555
|
|
|
|
|
|
|
if defined $type; |
|
556
|
|
|
|
|
|
|
|
|
557
|
6929
|
100
|
|
|
|
25749
|
if( $name !~ /^[\w:\.]+$/ ) { |
|
558
|
2
|
|
|
|
|
13
|
throw_exception( InvalidNameForType => name => $name ); |
|
559
|
|
|
|
|
|
|
} |
|
560
|
|
|
|
|
|
|
} |
|
561
|
|
|
|
|
|
|
|
|
562
|
6949
|
100
|
|
|
|
33617
|
my %opts = ( |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
name => $name, |
|
564
|
|
|
|
|
|
|
package_defined_in => $pkg_defined_in, |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
( $check ? ( constraint => $check ) : () ), |
|
567
|
|
|
|
|
|
|
( $message ? ( message => $message ) : () ), |
|
568
|
|
|
|
|
|
|
( $inlined ? ( inlined => $inlined ) : () ), |
|
569
|
|
|
|
|
|
|
); |
|
570
|
|
|
|
|
|
|
|
|
571
|
6949
|
|
|
|
|
9956
|
my $constraint; |
|
572
|
6949
|
100
|
66
|
|
|
28242
|
if ( |
|
|
|
100
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
defined $parent |
|
574
|
|
|
|
|
|
|
and $parent |
|
575
|
|
|
|
|
|
|
= blessed $parent |
|
576
|
|
|
|
|
|
|
? $parent |
|
577
|
|
|
|
|
|
|
: find_or_create_isa_type_constraint($parent) |
|
578
|
|
|
|
|
|
|
) { |
|
579
|
6533
|
|
|
|
|
21526
|
$constraint = $parent->create_child_type(%opts); |
|
580
|
|
|
|
|
|
|
} |
|
581
|
|
|
|
|
|
|
else { |
|
582
|
415
|
|
|
|
|
4354
|
$constraint = Moose::Meta::TypeConstraint->new(%opts); |
|
583
|
|
|
|
|
|
|
} |
|
584
|
|
|
|
|
|
|
|
|
585
|
6948
|
100
|
|
|
|
32667
|
$REGISTRY->add_type_constraint($constraint) |
|
586
|
|
|
|
|
|
|
if defined $name; |
|
587
|
|
|
|
|
|
|
|
|
588
|
6948
|
|
|
|
|
26547
|
return $constraint; |
|
589
|
|
|
|
|
|
|
} |
|
590
|
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
sub _install_type_coercions ($$) { |
|
592
|
44
|
|
|
44
|
|
136
|
my ( $type_name, $coercion_map ) = @_; |
|
593
|
44
|
|
|
|
|
132
|
my $type = find_type_constraint($type_name); |
|
594
|
44
|
100
|
|
|
|
167
|
( defined $type ) |
|
595
|
|
|
|
|
|
|
|| throw_exception( CannotFindType => type_name => $type_name ); |
|
596
|
|
|
|
|
|
|
|
|
597
|
43
|
100
|
|
|
|
1327
|
if ( $type->has_coercion ) { |
|
598
|
5
|
|
|
|
|
123
|
$type->coercion->add_type_coercions(@$coercion_map); |
|
599
|
|
|
|
|
|
|
} |
|
600
|
|
|
|
|
|
|
else { |
|
601
|
38
|
|
|
|
|
330
|
my $type_coercion = Moose::Meta::TypeCoercion->new( |
|
602
|
|
|
|
|
|
|
type_coercion_map => $coercion_map, |
|
603
|
|
|
|
|
|
|
type_constraint => $type |
|
604
|
|
|
|
|
|
|
); |
|
605
|
37
|
|
|
|
|
1021
|
$type->coercion($type_coercion); |
|
606
|
|
|
|
|
|
|
} |
|
607
|
|
|
|
|
|
|
} |
|
608
|
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
610
|
|
|
|
|
|
|
## type notation parsing ... |
|
611
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
612
|
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
{ |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
# All I have to say is mugwump++ cause I know |
|
616
|
|
|
|
|
|
|
# do not even have enough regexp-fu to be able |
|
617
|
|
|
|
|
|
|
# to have written this (I can only barely |
|
618
|
|
|
|
|
|
|
# understand it as it is) |
|
619
|
|
|
|
|
|
|
# - SL |
|
620
|
|
|
|
|
|
|
|
|
621
|
401
|
|
|
401
|
|
3632
|
use re "eval"; |
|
|
401
|
|
|
|
|
1014
|
|
|
|
401
|
|
|
|
|
162773
|
|
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
my $valid_chars = qr{[\w:\.]}; |
|
624
|
|
|
|
|
|
|
my $type_atom = qr{ (?>$valid_chars+) }x; |
|
625
|
|
|
|
|
|
|
my $ws = qr{ (?>\s*) }x; |
|
626
|
|
|
|
|
|
|
my $op_union = qr{ $ws \| $ws }x; |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
my ($type, $type_capture_parts, $type_with_parameter, $union, $any); |
|
629
|
|
|
|
|
|
|
if (Class::MOP::IS_RUNNING_ON_5_10) { |
|
630
|
|
|
|
|
|
|
my $type_pattern |
|
631
|
|
|
|
|
|
|
= q{ (?&type_atom) (?: \[ (?&ws) (?&any) (?&ws) \] )? }; |
|
632
|
|
|
|
|
|
|
my $type_capture_parts_pattern |
|
633
|
|
|
|
|
|
|
= q{ ((?&type_atom)) (?: \[ (?&ws) ((?&any)) (?&ws) \] )? }; |
|
634
|
|
|
|
|
|
|
my $type_with_parameter_pattern |
|
635
|
|
|
|
|
|
|
= q{ (?&type_atom) \[ (?&ws) (?&any) (?&ws) \] }; |
|
636
|
|
|
|
|
|
|
my $union_pattern |
|
637
|
|
|
|
|
|
|
= q{ (?&type) (?> (?: (?&op_union) (?&type) )+ ) }; |
|
638
|
|
|
|
|
|
|
my $any_pattern |
|
639
|
|
|
|
|
|
|
= q{ (?&type) | (?&union) }; |
|
640
|
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
my $defines = qr{(?(DEFINE) |
|
642
|
|
|
|
|
|
|
(?<valid_chars> $valid_chars) |
|
643
|
|
|
|
|
|
|
(?<type_atom> $type_atom) |
|
644
|
|
|
|
|
|
|
(?<ws> $ws) |
|
645
|
|
|
|
|
|
|
(?<op_union> $op_union) |
|
646
|
|
|
|
|
|
|
(?<type> $type_pattern) |
|
647
|
|
|
|
|
|
|
(?<type_capture_parts> $type_capture_parts_pattern) |
|
648
|
|
|
|
|
|
|
(?<type_with_parameter> $type_with_parameter_pattern) |
|
649
|
|
|
|
|
|
|
(?<union> $union_pattern) |
|
650
|
|
|
|
|
|
|
(?<any> $any_pattern) |
|
651
|
|
|
|
|
|
|
)}x; |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
$type = qr{ $type_pattern $defines }x; |
|
654
|
|
|
|
|
|
|
$type_capture_parts = qr{ $type_capture_parts_pattern $defines }x; |
|
655
|
|
|
|
|
|
|
$type_with_parameter = qr{ $type_with_parameter_pattern $defines }x; |
|
656
|
|
|
|
|
|
|
$union = qr{ $union_pattern $defines }x; |
|
657
|
|
|
|
|
|
|
$any = qr{ $any_pattern $defines }x; |
|
658
|
|
|
|
|
|
|
} |
|
659
|
|
|
|
|
|
|
else { |
|
660
|
|
|
|
|
|
|
$type |
|
661
|
|
|
|
|
|
|
= qr{ $type_atom (?: \[ $ws (??{$any}) $ws \] )? }x; |
|
662
|
|
|
|
|
|
|
$type_capture_parts |
|
663
|
|
|
|
|
|
|
= qr{ ($type_atom) (?: \[ $ws ((??{$any})) $ws \] )? }x; |
|
664
|
|
|
|
|
|
|
$type_with_parameter |
|
665
|
|
|
|
|
|
|
= qr{ $type_atom \[ $ws (??{$any}) $ws \] }x; |
|
666
|
|
|
|
|
|
|
$union |
|
667
|
|
|
|
|
|
|
= qr{ $type (?> (?: $op_union $type )+ ) }x; |
|
668
|
|
|
|
|
|
|
$any |
|
669
|
|
|
|
|
|
|
= qr{ $type | $union }x; |
|
670
|
|
|
|
|
|
|
} |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
sub _parse_parameterized_type_constraint { |
|
674
|
401
|
|
|
401
|
|
3270
|
{ no warnings 'void'; $any; } # force capture of interpolated lexical |
|
|
401
|
|
|
148
|
|
1000
|
|
|
|
401
|
|
|
|
|
40557
|
|
|
|
148
|
|
|
|
|
3241
|
|
|
|
148
|
|
|
|
|
238
|
|
|
675
|
148
|
|
|
|
|
8714
|
$_[0] =~ m{ $type_capture_parts }x; |
|
676
|
148
|
|
|
|
|
1063
|
return ( $1, $2 ); |
|
677
|
|
|
|
|
|
|
} |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
sub _detect_parameterized_type_constraint { |
|
680
|
401
|
|
|
401
|
|
3056
|
{ no warnings 'void'; $any; } # force capture of interpolated lexical |
|
|
401
|
|
|
429
|
|
987
|
|
|
|
401
|
|
|
|
|
32222
|
|
|
|
429
|
|
|
|
|
4064
|
|
|
|
429
|
|
|
|
|
799
|
|
|
681
|
429
|
|
|
|
|
32034
|
$_[0] =~ m{ ^ $type_with_parameter $ }x; |
|
682
|
|
|
|
|
|
|
} |
|
683
|
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
sub _parse_type_constraint_union { |
|
685
|
401
|
|
|
401
|
|
2987
|
{ no warnings 'void'; $any; } # force capture of interpolated lexical |
|
|
401
|
|
|
74
|
|
980
|
|
|
|
401
|
|
|
|
|
50369
|
|
|
|
74
|
|
|
|
|
6217
|
|
|
|
74
|
|
|
|
|
139
|
|
|
686
|
74
|
|
|
|
|
173
|
my $given = shift; |
|
687
|
74
|
|
|
|
|
133
|
my @rv; |
|
688
|
74
|
|
|
|
|
3023
|
while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) { |
|
689
|
159
|
|
|
|
|
1470
|
push @rv => $1; |
|
690
|
|
|
|
|
|
|
} |
|
691
|
74
|
100
|
|
|
|
330
|
( pos($given) eq length($given) ) |
|
692
|
|
|
|
|
|
|
|| throw_exception( CouldNotParseType => type => $given, |
|
693
|
|
|
|
|
|
|
position => pos($given) |
|
694
|
|
|
|
|
|
|
); |
|
695
|
73
|
|
|
|
|
343
|
@rv; |
|
696
|
|
|
|
|
|
|
} |
|
697
|
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
sub _detect_type_constraint_union { |
|
699
|
401
|
|
|
401
|
|
2943
|
{ no warnings 'void'; $any; } # force capture of interpolated lexical |
|
|
401
|
|
|
548
|
|
925
|
|
|
|
401
|
|
|
|
|
114428
|
|
|
|
548
|
|
|
|
|
7178
|
|
|
|
548
|
|
|
|
|
1104
|
|
|
700
|
548
|
|
|
|
|
63050
|
$_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x; |
|
701
|
|
|
|
|
|
|
} |
|
702
|
|
|
|
|
|
|
} |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
705
|
|
|
|
|
|
|
# define some basic built-in types |
|
706
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
# By making these classes immutable before creating all the types in |
|
709
|
|
|
|
|
|
|
# Moose::Util::TypeConstraints::Builtin , we avoid repeatedly calling the slow |
|
710
|
|
|
|
|
|
|
# MOP-based accessors. |
|
711
|
|
|
|
|
|
|
$_->make_immutable( |
|
712
|
|
|
|
|
|
|
inline_constructor => 1, |
|
713
|
|
|
|
|
|
|
constructor_name => "_new", |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
# these are Class::MOP accessors, so they need inlining |
|
716
|
|
|
|
|
|
|
inline_accessors => 1 |
|
717
|
|
|
|
|
|
|
) for grep { $_->is_mutable } |
|
718
|
|
|
|
|
|
|
map { Class::MOP::class_of($_) } |
|
719
|
|
|
|
|
|
|
qw( |
|
720
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint |
|
721
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Union |
|
722
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Parameterized |
|
723
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Parameterizable |
|
724
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Class |
|
725
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Role |
|
726
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Enum |
|
727
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::DuckType |
|
728
|
|
|
|
|
|
|
Moose::Meta::TypeConstraint::Registry |
|
729
|
|
|
|
|
|
|
); |
|
730
|
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
require Moose::Util::TypeConstraints::Builtins; |
|
732
|
|
|
|
|
|
|
Moose::Util::TypeConstraints::Builtins::define_builtins($REGISTRY); |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
my @PARAMETERIZABLE_TYPES |
|
735
|
|
|
|
|
|
|
= map { $REGISTRY->get_type_constraint($_) } qw[ScalarRef ArrayRef HashRef Maybe]; |
|
736
|
|
|
|
|
|
|
|
|
737
|
145
|
|
|
145
|
1
|
528
|
sub get_all_parameterizable_types {@PARAMETERIZABLE_TYPES} |
|
738
|
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
sub add_parameterizable_type { |
|
740
|
1
|
|
|
1
|
1
|
87
|
my $type = shift; |
|
741
|
1
|
50
|
33
|
|
|
12
|
( blessed $type |
|
742
|
|
|
|
|
|
|
&& $type->isa('Moose::Meta::TypeConstraint::Parameterizable') ) |
|
743
|
|
|
|
|
|
|
|| throw_exception( AddParameterizableTypeTakesParameterizableType => type_name => $type ); |
|
744
|
|
|
|
|
|
|
|
|
745
|
0
|
|
|
|
|
|
push @PARAMETERIZABLE_TYPES => $type; |
|
746
|
|
|
|
|
|
|
} |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
749
|
|
|
|
|
|
|
# end of built-in types ... |
|
750
|
|
|
|
|
|
|
## -------------------------------------------------------- |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
{ |
|
753
|
|
|
|
|
|
|
my @BUILTINS = list_all_type_constraints(); |
|
754
|
0
|
|
|
0
|
1
|
|
sub list_all_builtin_type_constraints {@BUILTINS} |
|
755
|
|
|
|
|
|
|
} |
|
756
|
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
1; |
|
758
|
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
# ABSTRACT: Type constraint system for Moose |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
__END__ |
|
762
|
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
=pod |
|
764
|
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
=encoding UTF-8 |
|
766
|
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=head1 NAME |
|
768
|
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
Moose::Util::TypeConstraints - Type constraint system for Moose |
|
770
|
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
=head1 VERSION |
|
772
|
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
version 2.2203 |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
|
778
|
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
subtype 'Natural', |
|
780
|
|
|
|
|
|
|
as 'Int', |
|
781
|
|
|
|
|
|
|
where { $_ > 0 }; |
|
782
|
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
subtype 'NaturalLessThanTen', |
|
784
|
|
|
|
|
|
|
as 'Natural', |
|
785
|
|
|
|
|
|
|
where { $_ < 10 }, |
|
786
|
|
|
|
|
|
|
message { "This number ($_) is not less than ten!" }; |
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
coerce 'Num', |
|
789
|
|
|
|
|
|
|
from 'Str', |
|
790
|
|
|
|
|
|
|
via { 0+$_ }; |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
class_type 'DateTimeClass', { class => 'DateTime' }; |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
role_type 'Barks', { role => 'Some::Library::Role::Barks' }; |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
enum 'RGBColors', [qw(red green blue)]; |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
union 'StringOrArray', [qw( String ArrayRef )]; |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
no Moose::Util::TypeConstraints; |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
803
|
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
This module provides Moose with the ability to create custom type |
|
805
|
|
|
|
|
|
|
constraints to be used in attribute definition. |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=head2 Important Caveat |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
This is B<NOT> a type system for Perl 5. These are type constraints, |
|
810
|
|
|
|
|
|
|
and they are not used by Moose unless you tell it to. No type |
|
811
|
|
|
|
|
|
|
inference is performed, expressions are not typed, etc. etc. etc. |
|
812
|
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
A type constraint is at heart a small "check if a value is valid" |
|
814
|
|
|
|
|
|
|
function. A constraint can be associated with an attribute. This |
|
815
|
|
|
|
|
|
|
simplifies parameter validation, and makes your code clearer to read, |
|
816
|
|
|
|
|
|
|
because you can refer to constraints by name. |
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
=head2 Slightly Less Important Caveat |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
It is B<always> a good idea to quote your type names. |
|
821
|
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
This prevents Perl from trying to execute the call as an indirect |
|
823
|
|
|
|
|
|
|
object call. This can be an issue when you have a subtype with the |
|
824
|
|
|
|
|
|
|
same name as a valid class. |
|
825
|
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
For instance: |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
subtype DateTime => as Object => where { $_->isa('DateTime') }; |
|
829
|
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
will I<just work>, while this: |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
use DateTime; |
|
833
|
|
|
|
|
|
|
subtype DateTime => as Object => where { $_->isa('DateTime') }; |
|
834
|
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
will fail silently and cause many headaches. The simple way to solve |
|
836
|
|
|
|
|
|
|
this, as well as future proof your subtypes from classes which have |
|
837
|
|
|
|
|
|
|
yet to have been created, is to quote the type name: |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
use DateTime; |
|
840
|
|
|
|
|
|
|
subtype 'DateTime', as 'Object', where { $_->isa('DateTime') }; |
|
841
|
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=head2 Default Type Constraints |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
This module also provides a simple hierarchy for Perl 5 types, here is |
|
845
|
|
|
|
|
|
|
that hierarchy represented visually. |
|
846
|
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
Any |
|
848
|
|
|
|
|
|
|
Item |
|
849
|
|
|
|
|
|
|
Bool |
|
850
|
|
|
|
|
|
|
Maybe[`a] |
|
851
|
|
|
|
|
|
|
Undef |
|
852
|
|
|
|
|
|
|
Defined |
|
853
|
|
|
|
|
|
|
Value |
|
854
|
|
|
|
|
|
|
Str |
|
855
|
|
|
|
|
|
|
Num |
|
856
|
|
|
|
|
|
|
Int |
|
857
|
|
|
|
|
|
|
ClassName |
|
858
|
|
|
|
|
|
|
RoleName |
|
859
|
|
|
|
|
|
|
Ref |
|
860
|
|
|
|
|
|
|
ScalarRef[`a] |
|
861
|
|
|
|
|
|
|
ArrayRef[`a] |
|
862
|
|
|
|
|
|
|
HashRef[`a] |
|
863
|
|
|
|
|
|
|
CodeRef |
|
864
|
|
|
|
|
|
|
RegexpRef |
|
865
|
|
|
|
|
|
|
GlobRef |
|
866
|
|
|
|
|
|
|
FileHandle |
|
867
|
|
|
|
|
|
|
Object |
|
868
|
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
B<NOTE:> Any type followed by a type parameter C<[`a]> can be |
|
870
|
|
|
|
|
|
|
parameterized, this means you can say: |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
ArrayRef[Int] # an array of integers |
|
873
|
|
|
|
|
|
|
HashRef[CodeRef] # a hash of str to CODE ref mappings |
|
874
|
|
|
|
|
|
|
ScalarRef[Int] # a reference to an integer |
|
875
|
|
|
|
|
|
|
Maybe[Str] # value may be a string, may be undefined |
|
876
|
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
If Moose finds a name in brackets that it does not recognize as an |
|
878
|
|
|
|
|
|
|
existing type, it assumes that this is a class name, for example |
|
879
|
|
|
|
|
|
|
C<ArrayRef[DateTime]>. |
|
880
|
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
B<NOTE:> Unless you parameterize a type, then it is invalid to include |
|
882
|
|
|
|
|
|
|
the square brackets. I.e. C<ArrayRef[]> will be treated as a new type |
|
883
|
|
|
|
|
|
|
name, I<not> as a parameterization of C<ArrayRef>. |
|
884
|
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
B<NOTE:> The C<Undef> type constraint for the most part works |
|
886
|
|
|
|
|
|
|
correctly now, but edge cases may still exist, please use it |
|
887
|
|
|
|
|
|
|
sparingly. |
|
888
|
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
B<NOTE:> The C<ClassName> type constraint does a complex package |
|
890
|
|
|
|
|
|
|
existence check. This means that your class B<must> be loaded for this |
|
891
|
|
|
|
|
|
|
type constraint to pass. |
|
892
|
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
B<NOTE:> The C<RoleName> constraint checks a string is a I<package |
|
894
|
|
|
|
|
|
|
name> which is a role, like C<'MyApp::Role::Comparable'>. |
|
895
|
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
=head2 Type Constraint Naming |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
Type names declared via this module can only contain alphanumeric |
|
899
|
|
|
|
|
|
|
characters, colons (:), and periods (.). |
|
900
|
|
|
|
|
|
|
|
|
901
|
|
|
|
|
|
|
Since the types created by this module are global, it is suggested |
|
902
|
|
|
|
|
|
|
that you namespace your types just as you would namespace your |
|
903
|
|
|
|
|
|
|
modules. So instead of creating a I<Color> type for your |
|
904
|
|
|
|
|
|
|
B<My::Graphics> module, you would call the type |
|
905
|
|
|
|
|
|
|
I<My::Graphics::Types::Color> instead. |
|
906
|
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
=head2 Use with Other Constraint Modules |
|
908
|
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
This module can play nicely with other constraint modules with some |
|
910
|
|
|
|
|
|
|
slight tweaking. The C<where> clause in types is expected to be a |
|
911
|
|
|
|
|
|
|
C<CODE> reference which checks its first argument and returns a |
|
912
|
|
|
|
|
|
|
boolean. Since most constraint modules work in a similar way, it |
|
913
|
|
|
|
|
|
|
should be simple to adapt them to work with Moose. |
|
914
|
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
For instance, this is how you could use it with |
|
916
|
|
|
|
|
|
|
L<Declare::Constraints::Simple> to declare a completely new type. |
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
type 'HashOfArrayOfObjects', |
|
919
|
|
|
|
|
|
|
where { |
|
920
|
|
|
|
|
|
|
IsHashRef( |
|
921
|
|
|
|
|
|
|
-keys => HasLength, |
|
922
|
|
|
|
|
|
|
-values => IsArrayRef(IsObject) |
|
923
|
|
|
|
|
|
|
)->(@_); |
|
924
|
|
|
|
|
|
|
}; |
|
925
|
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
For more examples see the F<t/examples/example_w_DCS.t> test |
|
927
|
|
|
|
|
|
|
file. |
|
928
|
|
|
|
|
|
|
|
|
929
|
|
|
|
|
|
|
Here is an example of using L<Test::Deep> and its non-test |
|
930
|
|
|
|
|
|
|
related C<eq_deeply> function. |
|
931
|
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
type 'ArrayOfHashOfBarsAndRandomNumbers', |
|
933
|
|
|
|
|
|
|
where { |
|
934
|
|
|
|
|
|
|
eq_deeply($_, |
|
935
|
|
|
|
|
|
|
array_each(subhashof({ |
|
936
|
|
|
|
|
|
|
bar => isa('Bar'), |
|
937
|
|
|
|
|
|
|
random_number => ignore() |
|
938
|
|
|
|
|
|
|
}))) |
|
939
|
|
|
|
|
|
|
}; |
|
940
|
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
For a complete example see the |
|
942
|
|
|
|
|
|
|
F<t/examples/example_w_TestDeep.t> test file. |
|
943
|
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
=head2 Error messages |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
Type constraints can also specify custom error messages, for when they fail to |
|
947
|
|
|
|
|
|
|
validate. This is provided as just another coderef, which receives the invalid |
|
948
|
|
|
|
|
|
|
value in C<$_>, as in: |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
subtype 'PositiveInt', |
|
951
|
|
|
|
|
|
|
as 'Int', |
|
952
|
|
|
|
|
|
|
where { $_ > 0 }, |
|
953
|
|
|
|
|
|
|
message { "$_ is not a positive integer!" }; |
|
954
|
|
|
|
|
|
|
|
|
955
|
|
|
|
|
|
|
If no message is specified, a default message will be used, which indicates |
|
956
|
|
|
|
|
|
|
which type constraint was being used and what value failed. If |
|
957
|
|
|
|
|
|
|
L<Devel::PartialDump> (version 0.14 or higher) is installed, it will be used to |
|
958
|
|
|
|
|
|
|
display the invalid value, otherwise it will just be printed as is. |
|
959
|
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
961
|
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
=head2 Type Constraint Constructors |
|
963
|
|
|
|
|
|
|
|
|
964
|
|
|
|
|
|
|
The following functions are used to create type constraints. They |
|
965
|
|
|
|
|
|
|
will also register the type constraints your create in a global |
|
966
|
|
|
|
|
|
|
registry that is used to look types up by name. |
|
967
|
|
|
|
|
|
|
|
|
968
|
|
|
|
|
|
|
See the L</SYNOPSIS> for an example of how to use these. |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
=head3 subtype 'Name', as 'Parent', where { } ... |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
This creates a named subtype. |
|
973
|
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
If you provide a parent that Moose does not recognize, it will |
|
975
|
|
|
|
|
|
|
automatically create a new class type constraint for this name. |
|
976
|
|
|
|
|
|
|
|
|
977
|
|
|
|
|
|
|
When creating a named type, the C<subtype> function should either be |
|
978
|
|
|
|
|
|
|
called with the sugar helpers (C<where>, C<message>, etc), or with a |
|
979
|
|
|
|
|
|
|
name and a hashref of parameters: |
|
980
|
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
subtype( 'Foo', { where => ..., message => ... } ); |
|
982
|
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
The valid hashref keys are C<as> (the parent), C<where>, C<message>, |
|
984
|
|
|
|
|
|
|
and C<inline_as>. |
|
985
|
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
=head3 subtype as 'Parent', where { } ... |
|
987
|
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
This creates an unnamed subtype and will return the type |
|
989
|
|
|
|
|
|
|
constraint meta-object, which will be an instance of |
|
990
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint>. |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
When creating an anonymous type, the C<subtype> function should either |
|
993
|
|
|
|
|
|
|
be called with the sugar helpers (C<where>, C<message>, etc), or with |
|
994
|
|
|
|
|
|
|
just a hashref of parameters: |
|
995
|
|
|
|
|
|
|
|
|
996
|
|
|
|
|
|
|
subtype( { where => ..., message => ... } ); |
|
997
|
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
=head3 class_type ($class, ?$options) |
|
999
|
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
Creates a new subtype of C<Object> with the name C<$class> and the |
|
1001
|
|
|
|
|
|
|
metaclass L<Moose::Meta::TypeConstraint::Class>. |
|
1002
|
|
|
|
|
|
|
|
|
1003
|
|
|
|
|
|
|
# Create a type called 'Box' which tests for objects which ->isa('Box') |
|
1004
|
|
|
|
|
|
|
class_type 'Box'; |
|
1005
|
|
|
|
|
|
|
|
|
1006
|
|
|
|
|
|
|
By default, the name of the type and the name of the class are the same, but |
|
1007
|
|
|
|
|
|
|
you can specify both separately. |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
# Create a type called 'Box' which tests for objects which ->isa('ObjectLibrary::Box'); |
|
1010
|
|
|
|
|
|
|
class_type 'Box', { class => 'ObjectLibrary::Box' }; |
|
1011
|
|
|
|
|
|
|
|
|
1012
|
|
|
|
|
|
|
=head3 role_type ($role, ?$options) |
|
1013
|
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
Creates a C<Role> type constraint with the name C<$role> and the |
|
1015
|
|
|
|
|
|
|
metaclass L<Moose::Meta::TypeConstraint::Role>. |
|
1016
|
|
|
|
|
|
|
|
|
1017
|
|
|
|
|
|
|
# Create a type called 'Walks' which tests for objects which ->does('Walks') |
|
1018
|
|
|
|
|
|
|
role_type 'Walks'; |
|
1019
|
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
By default, the name of the type and the name of the role are the same, but |
|
1021
|
|
|
|
|
|
|
you can specify both separately. |
|
1022
|
|
|
|
|
|
|
|
|
1023
|
|
|
|
|
|
|
# Create a type called 'Walks' which tests for objects which ->does('MooseX::Role::Walks'); |
|
1024
|
|
|
|
|
|
|
role_type 'Walks', { role => 'MooseX::Role::Walks' }; |
|
1025
|
|
|
|
|
|
|
|
|
1026
|
|
|
|
|
|
|
=head3 maybe_type ($type) |
|
1027
|
|
|
|
|
|
|
|
|
1028
|
|
|
|
|
|
|
Creates a type constraint for either C<undef> or something of the |
|
1029
|
|
|
|
|
|
|
given type. |
|
1030
|
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
=head3 duck_type ($name, \@methods) |
|
1032
|
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
This will create a subtype of Object and test to make sure the value |
|
1034
|
|
|
|
|
|
|
C<can()> do the methods in C<\@methods>. |
|
1035
|
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
This is intended as an easy way to accept non-Moose objects that |
|
1037
|
|
|
|
|
|
|
provide a certain interface. If you're using Moose classes, we |
|
1038
|
|
|
|
|
|
|
recommend that you use a C<requires>-only Role instead. |
|
1039
|
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
=head3 duck_type (\@methods) |
|
1041
|
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
If passed an ARRAY reference as the only parameter instead of the |
|
1043
|
|
|
|
|
|
|
C<$name>, C<\@methods> pair, this will create an unnamed duck type. |
|
1044
|
|
|
|
|
|
|
This can be used in an attribute definition like so: |
|
1045
|
|
|
|
|
|
|
|
|
1046
|
|
|
|
|
|
|
has 'cache' => ( |
|
1047
|
|
|
|
|
|
|
is => 'ro', |
|
1048
|
|
|
|
|
|
|
isa => duck_type( [qw( get_set )] ), |
|
1049
|
|
|
|
|
|
|
); |
|
1050
|
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
=head3 enum ($name, \@values) |
|
1052
|
|
|
|
|
|
|
|
|
1053
|
|
|
|
|
|
|
This will create a basic subtype for a given set of strings. |
|
1054
|
|
|
|
|
|
|
The resulting constraint will be a subtype of C<Str> and |
|
1055
|
|
|
|
|
|
|
will match any of the items in C<\@values>. It is case sensitive. |
|
1056
|
|
|
|
|
|
|
See the L</SYNOPSIS> for a simple example. |
|
1057
|
|
|
|
|
|
|
|
|
1058
|
|
|
|
|
|
|
B<NOTE:> This is not a true proper enum type, it is simply |
|
1059
|
|
|
|
|
|
|
a convenient constraint builder. |
|
1060
|
|
|
|
|
|
|
|
|
1061
|
|
|
|
|
|
|
=head3 enum (\@values) |
|
1062
|
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
If passed an ARRAY reference as the only parameter instead of the |
|
1064
|
|
|
|
|
|
|
C<$name>, C<\@values> pair, this will create an unnamed enum. This |
|
1065
|
|
|
|
|
|
|
can then be used in an attribute definition like so: |
|
1066
|
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
has 'sort_order' => ( |
|
1068
|
|
|
|
|
|
|
is => 'ro', |
|
1069
|
|
|
|
|
|
|
isa => enum([qw[ ascending descending ]]), |
|
1070
|
|
|
|
|
|
|
); |
|
1071
|
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
=head3 union ($name, \@constraints) |
|
1073
|
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
This will create a basic subtype where any of the provided constraints |
|
1075
|
|
|
|
|
|
|
may match in order to satisfy this constraint. |
|
1076
|
|
|
|
|
|
|
|
|
1077
|
|
|
|
|
|
|
=head3 union (\@constraints) |
|
1078
|
|
|
|
|
|
|
|
|
1079
|
|
|
|
|
|
|
If passed an ARRAY reference as the only parameter instead of the |
|
1080
|
|
|
|
|
|
|
C<$name>, C<\@constraints> pair, this will create an unnamed union. |
|
1081
|
|
|
|
|
|
|
This can then be used in an attribute definition like so: |
|
1082
|
|
|
|
|
|
|
|
|
1083
|
|
|
|
|
|
|
has 'items' => ( |
|
1084
|
|
|
|
|
|
|
is => 'ro', |
|
1085
|
|
|
|
|
|
|
isa => union([qw[ Str ArrayRef ]]), |
|
1086
|
|
|
|
|
|
|
); |
|
1087
|
|
|
|
|
|
|
|
|
1088
|
|
|
|
|
|
|
This is similar to the existing string union: |
|
1089
|
|
|
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
isa => 'Str|ArrayRef' |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
except that it supports anonymous elements as child constraints: |
|
1093
|
|
|
|
|
|
|
|
|
1094
|
|
|
|
|
|
|
has 'color' => ( |
|
1095
|
|
|
|
|
|
|
isa => 'ro', |
|
1096
|
|
|
|
|
|
|
isa => union([ 'Int', enum([qw[ red green blue ]]) ]), |
|
1097
|
|
|
|
|
|
|
); |
|
1098
|
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
=head3 as 'Parent' |
|
1100
|
|
|
|
|
|
|
|
|
1101
|
|
|
|
|
|
|
This is just sugar for the type constraint construction syntax. |
|
1102
|
|
|
|
|
|
|
|
|
1103
|
|
|
|
|
|
|
It takes a single argument, which is the name of a parent type. |
|
1104
|
|
|
|
|
|
|
|
|
1105
|
|
|
|
|
|
|
=head3 where { ... } |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
This is just sugar for the type constraint construction syntax. |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
It takes a subroutine reference as an argument. When the type |
|
1110
|
|
|
|
|
|
|
constraint is tested, the reference is run with the value to be tested |
|
1111
|
|
|
|
|
|
|
in C<$_>. This reference should return true or false to indicate |
|
1112
|
|
|
|
|
|
|
whether or not the constraint check passed. |
|
1113
|
|
|
|
|
|
|
|
|
1114
|
|
|
|
|
|
|
=head3 message { ... } |
|
1115
|
|
|
|
|
|
|
|
|
1116
|
|
|
|
|
|
|
This is just sugar for the type constraint construction syntax. |
|
1117
|
|
|
|
|
|
|
|
|
1118
|
|
|
|
|
|
|
It takes a subroutine reference as an argument. When the type |
|
1119
|
|
|
|
|
|
|
constraint fails, then the code block is run with the value provided |
|
1120
|
|
|
|
|
|
|
in C<$_>. This reference should return a string, which will be used in |
|
1121
|
|
|
|
|
|
|
the text of the exception thrown. |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
=head3 inline_as { ... } |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
This can be used to define a "hand optimized" inlinable version of your type |
|
1126
|
|
|
|
|
|
|
constraint. |
|
1127
|
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
You provide a subroutine which will be called I<as a method> on a |
|
1129
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint> object. It will receive a single parameter, the |
|
1130
|
|
|
|
|
|
|
name of the variable to check, typically something like C<"$_"> or C<"$_[0]">. |
|
1131
|
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
The subroutine should return a code string suitable for inlining. You can |
|
1133
|
|
|
|
|
|
|
assume that the check will be wrapped in parentheses when it is inlined. |
|
1134
|
|
|
|
|
|
|
|
|
1135
|
|
|
|
|
|
|
The inlined code should include any checks that your type's parent types |
|
1136
|
|
|
|
|
|
|
do. If your parent type constraint defines its own inlining, you can simply use |
|
1137
|
|
|
|
|
|
|
that to avoid repeating code. For example, here is the inlining code for the |
|
1138
|
|
|
|
|
|
|
C<Value> type, which is a subtype of C<Defined>: |
|
1139
|
|
|
|
|
|
|
|
|
1140
|
|
|
|
|
|
|
sub { |
|
1141
|
|
|
|
|
|
|
$_[0]->parent()->_inline_check($_[1]) |
|
1142
|
|
|
|
|
|
|
. ' && !ref(' . $_[1] . ')' |
|
1143
|
|
|
|
|
|
|
} |
|
1144
|
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
=head3 type 'Name', where { } ... |
|
1146
|
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
This creates a base type, which has no parent. |
|
1148
|
|
|
|
|
|
|
|
|
1149
|
|
|
|
|
|
|
The C<type> function should either be called with the sugar helpers |
|
1150
|
|
|
|
|
|
|
(C<where>, C<message>, etc), or with a name and a hashref of |
|
1151
|
|
|
|
|
|
|
parameters: |
|
1152
|
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
type( 'Foo', { where => ..., message => ... } ); |
|
1154
|
|
|
|
|
|
|
|
|
1155
|
|
|
|
|
|
|
The valid hashref keys are C<where>, C<message>, and C<inlined_as>. |
|
1156
|
|
|
|
|
|
|
|
|
1157
|
|
|
|
|
|
|
=head2 Type Constraint Utilities |
|
1158
|
|
|
|
|
|
|
|
|
1159
|
|
|
|
|
|
|
=head3 match_on_type $value => ( $type => \&action, ... ?\&default ) |
|
1160
|
|
|
|
|
|
|
|
|
1161
|
|
|
|
|
|
|
This is a utility function for doing simple type based dispatching similar to |
|
1162
|
|
|
|
|
|
|
match/case in OCaml and case/of in Haskell. It is not as featureful as those |
|
1163
|
|
|
|
|
|
|
languages, nor does not it support any kind of automatic destructuring |
|
1164
|
|
|
|
|
|
|
bind. Here is a simple Perl pretty printer dispatching over the core Moose |
|
1165
|
|
|
|
|
|
|
types. |
|
1166
|
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
sub ppprint { |
|
1168
|
|
|
|
|
|
|
my $x = shift; |
|
1169
|
|
|
|
|
|
|
match_on_type $x => ( |
|
1170
|
|
|
|
|
|
|
HashRef => sub { |
|
1171
|
|
|
|
|
|
|
my $hash = shift; |
|
1172
|
|
|
|
|
|
|
'{ ' |
|
1173
|
|
|
|
|
|
|
. ( |
|
1174
|
|
|
|
|
|
|
join ", " => map { $_ . ' => ' . ppprint( $hash->{$_} ) } |
|
1175
|
|
|
|
|
|
|
sort keys %$hash |
|
1176
|
|
|
|
|
|
|
) . ' }'; |
|
1177
|
|
|
|
|
|
|
}, |
|
1178
|
|
|
|
|
|
|
ArrayRef => sub { |
|
1179
|
|
|
|
|
|
|
my $array = shift; |
|
1180
|
|
|
|
|
|
|
'[ ' . ( join ", " => map { ppprint($_) } @$array ) . ' ]'; |
|
1181
|
|
|
|
|
|
|
}, |
|
1182
|
|
|
|
|
|
|
CodeRef => sub {'sub { ... }'}, |
|
1183
|
|
|
|
|
|
|
RegexpRef => sub { 'qr/' . $_ . '/' }, |
|
1184
|
|
|
|
|
|
|
GlobRef => sub { '*' . B::svref_2object($_)->NAME }, |
|
1185
|
|
|
|
|
|
|
Object => sub { $_->can('to_string') ? $_->to_string : $_ }, |
|
1186
|
|
|
|
|
|
|
ScalarRef => sub { '\\' . ppprint( ${$_} ) }, |
|
1187
|
|
|
|
|
|
|
Num => sub {$_}, |
|
1188
|
|
|
|
|
|
|
Str => sub { '"' . $_ . '"' }, |
|
1189
|
|
|
|
|
|
|
Undef => sub {'undef'}, |
|
1190
|
|
|
|
|
|
|
=> sub { die "I don't know what $_ is" } |
|
1191
|
|
|
|
|
|
|
); |
|
1192
|
|
|
|
|
|
|
} |
|
1193
|
|
|
|
|
|
|
|
|
1194
|
|
|
|
|
|
|
Or a simple JSON serializer: |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
sub to_json { |
|
1197
|
|
|
|
|
|
|
my $x = shift; |
|
1198
|
|
|
|
|
|
|
match_on_type $x => ( |
|
1199
|
|
|
|
|
|
|
HashRef => sub { |
|
1200
|
|
|
|
|
|
|
my $hash = shift; |
|
1201
|
|
|
|
|
|
|
'{ ' |
|
1202
|
|
|
|
|
|
|
. ( |
|
1203
|
|
|
|
|
|
|
join ", " => |
|
1204
|
|
|
|
|
|
|
map { '"' . $_ . '" : ' . to_json( $hash->{$_} ) } |
|
1205
|
|
|
|
|
|
|
sort keys %$hash |
|
1206
|
|
|
|
|
|
|
) . ' }'; |
|
1207
|
|
|
|
|
|
|
}, |
|
1208
|
|
|
|
|
|
|
ArrayRef => sub { |
|
1209
|
|
|
|
|
|
|
my $array = shift; |
|
1210
|
|
|
|
|
|
|
'[ ' . ( join ", " => map { to_json($_) } @$array ) . ' ]'; |
|
1211
|
|
|
|
|
|
|
}, |
|
1212
|
|
|
|
|
|
|
Num => sub {$_}, |
|
1213
|
|
|
|
|
|
|
Str => sub { '"' . $_ . '"' }, |
|
1214
|
|
|
|
|
|
|
Undef => sub {'null'}, |
|
1215
|
|
|
|
|
|
|
=> sub { die "$_ is not acceptable json type" } |
|
1216
|
|
|
|
|
|
|
); |
|
1217
|
|
|
|
|
|
|
} |
|
1218
|
|
|
|
|
|
|
|
|
1219
|
|
|
|
|
|
|
The matcher is done by mapping a C<$type> to an C<\&action>. The C<$type> can |
|
1220
|
|
|
|
|
|
|
be either a string type or a L<Moose::Meta::TypeConstraint> object, and |
|
1221
|
|
|
|
|
|
|
C<\&action> is a subroutine reference. This function will dispatch on the |
|
1222
|
|
|
|
|
|
|
first match for C<$value>. It is possible to have a catch-all by providing an |
|
1223
|
|
|
|
|
|
|
additional subroutine reference as the final argument to C<match_on_type>. |
|
1224
|
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
=head2 Type Coercion Constructors |
|
1226
|
|
|
|
|
|
|
|
|
1227
|
|
|
|
|
|
|
You can define coercions for type constraints, which allow you to |
|
1228
|
|
|
|
|
|
|
automatically transform values to something valid for the type |
|
1229
|
|
|
|
|
|
|
constraint. If you ask your accessor to coerce by adding the option C<< coerce => 1 >>, then Moose will run |
|
1230
|
|
|
|
|
|
|
the type-coercion code first, followed by the type constraint |
|
1231
|
|
|
|
|
|
|
check. This feature should be used carefully as it is very powerful |
|
1232
|
|
|
|
|
|
|
and could easily take off a limb if you are not careful. |
|
1233
|
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
See the L</SYNOPSIS> for an example of how to use these. |
|
1235
|
|
|
|
|
|
|
|
|
1236
|
|
|
|
|
|
|
=head3 coerce 'Name', from 'OtherName', via { ... } |
|
1237
|
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
This defines a coercion from one type to another. The C<Name> argument |
|
1239
|
|
|
|
|
|
|
is the type you are coercing I<to>. |
|
1240
|
|
|
|
|
|
|
|
|
1241
|
|
|
|
|
|
|
To define multiple coercions, supply more sets of from/via pairs: |
|
1242
|
|
|
|
|
|
|
|
|
1243
|
|
|
|
|
|
|
coerce 'Name', |
|
1244
|
|
|
|
|
|
|
from 'OtherName', via { ... }, |
|
1245
|
|
|
|
|
|
|
from 'ThirdName', via { ... }; |
|
1246
|
|
|
|
|
|
|
|
|
1247
|
|
|
|
|
|
|
=head3 from 'OtherName' |
|
1248
|
|
|
|
|
|
|
|
|
1249
|
|
|
|
|
|
|
This is just sugar for the type coercion construction syntax. |
|
1250
|
|
|
|
|
|
|
|
|
1251
|
|
|
|
|
|
|
It takes a single type name (or type object), which is the type being |
|
1252
|
|
|
|
|
|
|
coerced I<from>. |
|
1253
|
|
|
|
|
|
|
|
|
1254
|
|
|
|
|
|
|
=head3 via { ... } |
|
1255
|
|
|
|
|
|
|
|
|
1256
|
|
|
|
|
|
|
This is just sugar for the type coercion construction syntax. |
|
1257
|
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
It takes a subroutine reference. This reference will be called with |
|
1259
|
|
|
|
|
|
|
the value to be coerced in C<$_>. It is expected to return a new value |
|
1260
|
|
|
|
|
|
|
of the proper type for the coercion. |
|
1261
|
|
|
|
|
|
|
|
|
1262
|
|
|
|
|
|
|
=head2 Creating and Finding Type Constraints |
|
1263
|
|
|
|
|
|
|
|
|
1264
|
|
|
|
|
|
|
These are additional functions for creating and finding type |
|
1265
|
|
|
|
|
|
|
constraints. Most of these functions are not available for |
|
1266
|
|
|
|
|
|
|
importing. The ones that are importable as specified. |
|
1267
|
|
|
|
|
|
|
|
|
1268
|
|
|
|
|
|
|
=head3 find_type_constraint($type_name) |
|
1269
|
|
|
|
|
|
|
|
|
1270
|
|
|
|
|
|
|
This function can be used to locate the L<Moose::Meta::TypeConstraint> |
|
1271
|
|
|
|
|
|
|
object for a named type. |
|
1272
|
|
|
|
|
|
|
|
|
1273
|
|
|
|
|
|
|
This function is importable. |
|
1274
|
|
|
|
|
|
|
|
|
1275
|
|
|
|
|
|
|
=head3 register_type_constraint($type_object) |
|
1276
|
|
|
|
|
|
|
|
|
1277
|
|
|
|
|
|
|
This function will register a L<Moose::Meta::TypeConstraint> with the |
|
1278
|
|
|
|
|
|
|
global type registry. |
|
1279
|
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
This function is importable. |
|
1281
|
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
=head3 normalize_type_constraint_name($type_constraint_name) |
|
1283
|
|
|
|
|
|
|
|
|
1284
|
|
|
|
|
|
|
This method takes a type constraint name and returns the normalized |
|
1285
|
|
|
|
|
|
|
form. This removes any whitespace in the string. |
|
1286
|
|
|
|
|
|
|
|
|
1287
|
|
|
|
|
|
|
=head3 create_type_constraint_union($pipe_separated_types | @type_constraint_names) |
|
1288
|
|
|
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
=head3 create_named_type_constraint_union($name, $pipe_separated_types | @type_constraint_names) |
|
1290
|
|
|
|
|
|
|
|
|
1291
|
|
|
|
|
|
|
This can take a union type specification like C<'Int|ArrayRef[Int]'>, |
|
1292
|
|
|
|
|
|
|
or a list of names. It returns a new |
|
1293
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Union> object. |
|
1294
|
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
=head3 create_parameterized_type_constraint($type_name) |
|
1296
|
|
|
|
|
|
|
|
|
1297
|
|
|
|
|
|
|
Given a C<$type_name> in the form of C<'BaseType[ContainerType]'>, |
|
1298
|
|
|
|
|
|
|
this will create a new L<Moose::Meta::TypeConstraint::Parameterized> |
|
1299
|
|
|
|
|
|
|
object. The C<BaseType> must already exist as a parameterizable |
|
1300
|
|
|
|
|
|
|
type. |
|
1301
|
|
|
|
|
|
|
|
|
1302
|
|
|
|
|
|
|
=head3 create_class_type_constraint($class, $options) |
|
1303
|
|
|
|
|
|
|
|
|
1304
|
|
|
|
|
|
|
Given a class name this function will create a new |
|
1305
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Class> object for that class name. |
|
1306
|
|
|
|
|
|
|
|
|
1307
|
|
|
|
|
|
|
The C<$options> is a hash reference that will be passed to the |
|
1308
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Class> constructor (as a hash). |
|
1309
|
|
|
|
|
|
|
|
|
1310
|
|
|
|
|
|
|
=head3 create_role_type_constraint($role, $options) |
|
1311
|
|
|
|
|
|
|
|
|
1312
|
|
|
|
|
|
|
Given a role name this function will create a new |
|
1313
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Role> object for that role name. |
|
1314
|
|
|
|
|
|
|
|
|
1315
|
|
|
|
|
|
|
The C<$options> is a hash reference that will be passed to the |
|
1316
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Role> constructor (as a hash). |
|
1317
|
|
|
|
|
|
|
|
|
1318
|
|
|
|
|
|
|
=head3 create_enum_type_constraint($name, $values) |
|
1319
|
|
|
|
|
|
|
|
|
1320
|
|
|
|
|
|
|
Given a enum name this function will create a new |
|
1321
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::Enum> object for that enum name. |
|
1322
|
|
|
|
|
|
|
|
|
1323
|
|
|
|
|
|
|
=head3 create_duck_type_constraint($name, $methods) |
|
1324
|
|
|
|
|
|
|
|
|
1325
|
|
|
|
|
|
|
Given a duck type name this function will create a new |
|
1326
|
|
|
|
|
|
|
L<Moose::Meta::TypeConstraint::DuckType> object for that enum name. |
|
1327
|
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
=head3 find_or_parse_type_constraint($type_name) |
|
1329
|
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
Given a type name, this first attempts to find a matching constraint |
|
1331
|
|
|
|
|
|
|
in the global registry. |
|
1332
|
|
|
|
|
|
|
|
|
1333
|
|
|
|
|
|
|
If the type name is a union or parameterized type, it will create a |
|
1334
|
|
|
|
|
|
|
new object of the appropriate, but if given a "regular" type that does |
|
1335
|
|
|
|
|
|
|
not yet exist, it simply returns false. |
|
1336
|
|
|
|
|
|
|
|
|
1337
|
|
|
|
|
|
|
When given a union or parameterized type, the member or base type must |
|
1338
|
|
|
|
|
|
|
already exist. |
|
1339
|
|
|
|
|
|
|
|
|
1340
|
|
|
|
|
|
|
If it creates a new union or parameterized type, it will add it to the |
|
1341
|
|
|
|
|
|
|
global registry. |
|
1342
|
|
|
|
|
|
|
|
|
1343
|
|
|
|
|
|
|
=head3 find_or_create_isa_type_constraint($type_name) |
|
1344
|
|
|
|
|
|
|
|
|
1345
|
|
|
|
|
|
|
=head3 find_or_create_does_type_constraint($type_name) |
|
1346
|
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
These functions will first call C<find_or_parse_type_constraint>. If |
|
1348
|
|
|
|
|
|
|
that function does not return a type, a new type object will |
|
1349
|
|
|
|
|
|
|
be created. |
|
1350
|
|
|
|
|
|
|
|
|
1351
|
|
|
|
|
|
|
The C<isa> variant will use C<create_class_type_constraint> and the |
|
1352
|
|
|
|
|
|
|
C<does> variant will use C<create_role_type_constraint>. |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
=head3 get_type_constraint_registry |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
Returns the L<Moose::Meta::TypeConstraint::Registry> object which |
|
1357
|
|
|
|
|
|
|
keeps track of all type constraints. |
|
1358
|
|
|
|
|
|
|
|
|
1359
|
|
|
|
|
|
|
=head3 list_all_type_constraints |
|
1360
|
|
|
|
|
|
|
|
|
1361
|
|
|
|
|
|
|
This will return a list of type constraint names in the global |
|
1362
|
|
|
|
|
|
|
registry. You can then fetch the actual type object using |
|
1363
|
|
|
|
|
|
|
C<find_type_constraint($type_name)>. |
|
1364
|
|
|
|
|
|
|
|
|
1365
|
|
|
|
|
|
|
=head3 list_all_builtin_type_constraints |
|
1366
|
|
|
|
|
|
|
|
|
1367
|
|
|
|
|
|
|
This will return a list of builtin type constraints, meaning those |
|
1368
|
|
|
|
|
|
|
which are defined in this module. See the L<Default Type Constraints> |
|
1369
|
|
|
|
|
|
|
section for a complete list. |
|
1370
|
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
=head3 export_type_constraints_as_functions |
|
1372
|
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
This will export all the current type constraints as functions into |
|
1374
|
|
|
|
|
|
|
the caller's namespace (C<Int()>, C<Str()>, etc). Right now, this is |
|
1375
|
|
|
|
|
|
|
mostly used for testing, but it might prove useful to others. |
|
1376
|
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
=head3 get_all_parameterizable_types |
|
1378
|
|
|
|
|
|
|
|
|
1379
|
|
|
|
|
|
|
This returns all the parameterizable types that have been registered, |
|
1380
|
|
|
|
|
|
|
as a list of type objects. |
|
1381
|
|
|
|
|
|
|
|
|
1382
|
|
|
|
|
|
|
=head3 add_parameterizable_type($type) |
|
1383
|
|
|
|
|
|
|
|
|
1384
|
|
|
|
|
|
|
Adds C<$type> to the list of parameterizable types |
|
1385
|
|
|
|
|
|
|
|
|
1386
|
|
|
|
|
|
|
=head1 BUGS |
|
1387
|
|
|
|
|
|
|
|
|
1388
|
|
|
|
|
|
|
See L<Moose/BUGS> for details on reporting bugs. |
|
1389
|
|
|
|
|
|
|
|
|
1390
|
|
|
|
|
|
|
=head1 AUTHORS |
|
1391
|
|
|
|
|
|
|
|
|
1392
|
|
|
|
|
|
|
=over 4 |
|
1393
|
|
|
|
|
|
|
|
|
1394
|
|
|
|
|
|
|
=item * |
|
1395
|
|
|
|
|
|
|
|
|
1396
|
|
|
|
|
|
|
Stevan Little <stevan@cpan.org> |
|
1397
|
|
|
|
|
|
|
|
|
1398
|
|
|
|
|
|
|
=item * |
|
1399
|
|
|
|
|
|
|
|
|
1400
|
|
|
|
|
|
|
Dave Rolsky <autarch@urth.org> |
|
1401
|
|
|
|
|
|
|
|
|
1402
|
|
|
|
|
|
|
=item * |
|
1403
|
|
|
|
|
|
|
|
|
1404
|
|
|
|
|
|
|
Jesse Luehrs <doy@cpan.org> |
|
1405
|
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
=item * |
|
1407
|
|
|
|
|
|
|
|
|
1408
|
|
|
|
|
|
|
Shawn M Moore <sartak@cpan.org> |
|
1409
|
|
|
|
|
|
|
|
|
1410
|
|
|
|
|
|
|
=item * |
|
1411
|
|
|
|
|
|
|
|
|
1412
|
|
|
|
|
|
|
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org> |
|
1413
|
|
|
|
|
|
|
|
|
1414
|
|
|
|
|
|
|
=item * |
|
1415
|
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
|
1417
|
|
|
|
|
|
|
|
|
1418
|
|
|
|
|
|
|
=item * |
|
1419
|
|
|
|
|
|
|
|
|
1420
|
|
|
|
|
|
|
Florian Ragwitz <rafl@debian.org> |
|
1421
|
|
|
|
|
|
|
|
|
1422
|
|
|
|
|
|
|
=item * |
|
1423
|
|
|
|
|
|
|
|
|
1424
|
|
|
|
|
|
|
Hans Dieter Pearcey <hdp@cpan.org> |
|
1425
|
|
|
|
|
|
|
|
|
1426
|
|
|
|
|
|
|
=item * |
|
1427
|
|
|
|
|
|
|
|
|
1428
|
|
|
|
|
|
|
Chris Prather <chris@prather.org> |
|
1429
|
|
|
|
|
|
|
|
|
1430
|
|
|
|
|
|
|
=item * |
|
1431
|
|
|
|
|
|
|
|
|
1432
|
|
|
|
|
|
|
Matt S Trout <mstrout@cpan.org> |
|
1433
|
|
|
|
|
|
|
|
|
1434
|
|
|
|
|
|
|
=back |
|
1435
|
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
1437
|
|
|
|
|
|
|
|
|
1438
|
|
|
|
|
|
|
This software is copyright (c) 2006 by Infinity Interactive, Inc. |
|
1439
|
|
|
|
|
|
|
|
|
1440
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
1441
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
1442
|
|
|
|
|
|
|
|
|
1443
|
|
|
|
|
|
|
=cut |