line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Declare::Constraints::Simple::Library::Array - Array Constraints |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Declare::Constraints::Simple::Library::Array; |
8
|
12
|
|
|
12
|
|
78
|
use warnings; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
452
|
|
9
|
12
|
|
|
12
|
|
68
|
use strict; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
523
|
|
10
|
|
|
|
|
|
|
|
11
|
12
|
|
|
12
|
|
72
|
use Declare::Constraints::Simple-Library; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
102
|
|
12
|
12
|
|
|
12
|
|
76
|
use Carp::Clan qw(^Declare::Constraints::Simple); |
|
12
|
|
|
|
|
25
|
|
|
12
|
|
|
|
|
118
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# accept a list of pairs |
17
|
|
|
|
|
|
|
my $pairs_validation = IsArrayRef( HasArraySize(2,2) ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# integer => object pairs |
20
|
|
|
|
|
|
|
my $pairs = And( OnEvenElements(IsInt), |
21
|
|
|
|
|
|
|
OnOddElements(IsObject) ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# a three element array |
24
|
|
|
|
|
|
|
my $tri = And( HasArraySize(3,3), |
25
|
|
|
|
|
|
|
OnArrayElements(0, IsInt, |
26
|
|
|
|
|
|
|
1, IsDefined, |
27
|
|
|
|
|
|
|
2, IsClass) ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module contains all constraints that can be applied to array |
32
|
|
|
|
|
|
|
references. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 CONSTRAINTS |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 HasArraySize([$min, [$max]]) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
With C<$min> defaulting to 1. So a specification of |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $profile = HasArraySize; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
checks for at least one value. To force an exact size of the array, |
43
|
|
|
|
|
|
|
specify the same values for both: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $profile = HasArraySize(3, 3); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=cut |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
constraint 'HasArraySize', |
50
|
|
|
|
|
|
|
sub { |
51
|
|
|
|
|
|
|
my ($min, $max) = @_; |
52
|
|
|
|
|
|
|
$min = 1 unless defined $min; |
53
|
|
|
|
|
|
|
return sub { |
54
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
55
|
|
|
|
|
|
|
return _false('Not an ArrayRef') |
56
|
|
|
|
|
|
|
unless ref($_[0]) eq 'ARRAY'; |
57
|
|
|
|
|
|
|
return _false("Less than $min Array elements") |
58
|
|
|
|
|
|
|
unless scalar(@{$_[0]}) >= $min; |
59
|
|
|
|
|
|
|
return _true |
60
|
|
|
|
|
|
|
unless $max; |
61
|
|
|
|
|
|
|
return _false("More than $max Array elements") |
62
|
|
|
|
|
|
|
unless scalar(@{$_[0]}) <= $max; |
63
|
|
|
|
|
|
|
return _true; |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
}; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 OnArrayElements($key => $constraint, $key => $constraint, ...) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Applies the the C<$constraint>s to the corresponding C<$key>s if they are |
70
|
|
|
|
|
|
|
present. For required keys see C. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
constraint 'OnArrayElements', |
75
|
|
|
|
|
|
|
sub { |
76
|
|
|
|
|
|
|
my %keymap = @_; |
77
|
|
|
|
|
|
|
my @keys = sort keys %keymap; |
78
|
|
|
|
|
|
|
for (@keys) { |
79
|
|
|
|
|
|
|
croak "Not an array index: $_" if $_ =~ /\D/; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return sub { |
83
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
84
|
|
|
|
|
|
|
return _false('Not an ArrayRef') |
85
|
|
|
|
|
|
|
unless ref($_[0]) eq 'ARRAY'; |
86
|
|
|
|
|
|
|
for my $k (@keys) { |
87
|
|
|
|
|
|
|
last if $k > $#{$_[0]}; |
88
|
|
|
|
|
|
|
my $r = $keymap{$k}->($_[0][$k]); |
89
|
|
|
|
|
|
|
_info($k); |
90
|
|
|
|
|
|
|
return $r unless $r->is_valid; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
return _true; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 OnEvenElements($constraint) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Runs the constraint on all even elements of an array. See also |
99
|
|
|
|
|
|
|
C. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
constraint 'OnEvenElements', |
104
|
|
|
|
|
|
|
sub { |
105
|
|
|
|
|
|
|
my ($c) = @_; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
return sub { |
108
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
109
|
|
|
|
|
|
|
return _false('Not an ArrayRef') |
110
|
|
|
|
|
|
|
unless ref($_[0]) eq 'ARRAY'; |
111
|
|
|
|
|
|
|
my $p = 0; |
112
|
|
|
|
|
|
|
while ($p <= $#{$_[0]}) { |
113
|
|
|
|
|
|
|
my $r = $c->($_[0][$p]); |
114
|
|
|
|
|
|
|
_info($p); |
115
|
|
|
|
|
|
|
return $r unless $r->is_valid; |
116
|
|
|
|
|
|
|
$p += 2; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
return _true; |
119
|
|
|
|
|
|
|
}; |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 OnOddElements($constraint) |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Runs the constraint on all odd elements of an array. See also |
126
|
|
|
|
|
|
|
C. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
constraint 'OnOddElements', |
131
|
|
|
|
|
|
|
sub { |
132
|
|
|
|
|
|
|
my ($c) = @_; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
return sub { |
135
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
136
|
|
|
|
|
|
|
return _false('Not an ArrayRef') |
137
|
|
|
|
|
|
|
unless ref($_[0]) eq 'ARRAY'; |
138
|
|
|
|
|
|
|
my $p = 1; |
139
|
|
|
|
|
|
|
while ($p <= $#{$_[0]}) { |
140
|
|
|
|
|
|
|
my $r = $c->($_[0][$p]); |
141
|
|
|
|
|
|
|
_info($p); |
142
|
|
|
|
|
|
|
return $r unless $r->is_valid; |
143
|
|
|
|
|
|
|
$p += 2; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
return _true; |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 SEE ALSO |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
L, L |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Robert 'phaylon' Sedlacek Cphaylon@dunkelheit.atE> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This module is free software, you can redistribute it and/or modify it |
160
|
|
|
|
|
|
|
under the same terms as perl itself. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |