line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mo::utils; |
2
|
|
|
|
|
|
|
|
3
|
15
|
|
|
15
|
|
231725
|
use base qw(Exporter); |
|
15
|
|
|
|
|
122
|
|
|
15
|
|
|
|
|
1661
|
|
4
|
15
|
|
|
15
|
|
124
|
use strict; |
|
15
|
|
|
|
|
25
|
|
|
15
|
|
|
|
|
334
|
|
5
|
15
|
|
|
15
|
|
128
|
use warnings; |
|
15
|
|
|
|
|
25
|
|
|
15
|
|
|
|
|
503
|
|
6
|
|
|
|
|
|
|
|
7
|
15
|
|
|
15
|
|
7081
|
use Error::Pure qw(err); |
|
15
|
|
|
|
|
37091
|
|
|
15
|
|
|
|
|
280
|
|
8
|
15
|
|
|
15
|
|
1037
|
use List::Util qw(none); |
|
15
|
|
|
|
|
31
|
|
|
15
|
|
|
|
|
1964
|
|
9
|
15
|
|
|
15
|
|
97
|
use Readonly; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
703
|
|
10
|
15
|
|
|
15
|
|
98
|
use Scalar::Util qw(blessed); |
|
15
|
|
|
|
|
26
|
|
|
15
|
|
|
|
|
36303
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Readonly::Array our @EXPORT_OK => qw(check_array check_array_object check_array_required |
13
|
|
|
|
|
|
|
check_bool check_code check_isa check_length check_number check_number_of_items |
14
|
|
|
|
|
|
|
check_regexp check_required check_string_begin check_strings); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = 0.19; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub check_array { |
19
|
11
|
|
|
11
|
1
|
3649
|
my ($self, $key) = @_; |
20
|
|
|
|
|
|
|
|
21
|
11
|
100
|
|
|
|
31
|
if (! exists $self->{$key}) { |
22
|
1
|
|
|
|
|
3
|
return; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
10
|
100
|
|
|
|
36
|
if (ref $self->{$key} ne 'ARRAY') { |
26
|
|
|
|
|
|
|
err "Parameter '".$key."' must be a array.", |
27
|
|
|
|
|
|
|
'Value', $self->{$key}, |
28
|
5
|
|
|
|
|
36
|
'Reference', (ref $self->{$key}), |
29
|
|
|
|
|
|
|
; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
5
|
|
|
|
|
11
|
return; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub check_array_object { |
36
|
4
|
|
|
4
|
1
|
4447
|
my ($self, $key, $class, $class_name) = @_; |
37
|
|
|
|
|
|
|
|
38
|
4
|
100
|
|
|
|
13
|
if (! exists $self->{$key}) { |
39
|
1
|
|
|
|
|
21
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
3
|
|
|
|
|
12
|
check_array($self, $key); |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
4
|
foreach my $obj (@{$self->{$key}}) { |
|
2
|
|
|
|
|
6
|
|
45
|
2
|
100
|
|
|
|
18
|
if (! $obj->isa($class)) { |
46
|
1
|
|
|
|
|
16
|
err $class_name." isn't '".$class."' object."; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
3
|
return; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub check_array_required { |
54
|
5
|
|
|
5
|
1
|
6051
|
my ($self, $key) = @_; |
55
|
|
|
|
|
|
|
|
56
|
5
|
100
|
|
|
|
17
|
if (! exists $self->{$key}) { |
57
|
1
|
|
|
|
|
6
|
err "Parameter '$key' is required."; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
4
|
|
|
|
|
9
|
check_array($self, $key); |
61
|
|
|
|
|
|
|
|
62
|
2
|
100
|
|
|
|
4
|
if (! @{$self->{$key}}) { |
|
2
|
|
|
|
|
6
|
|
63
|
1
|
|
|
|
|
5
|
err "Parameter '".$key."' with array must have at least one item."; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
12
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub check_bool { |
70
|
6
|
|
|
6
|
1
|
5652
|
my ($self, $key) = @_; |
71
|
|
|
|
|
|
|
|
72
|
6
|
100
|
|
|
|
16
|
_check_key($self, $key) && return; |
73
|
|
|
|
|
|
|
|
74
|
4
|
100
|
100
|
|
|
44
|
if ($self->{$key} !~ m/^\d+$/ms || ($self->{$key} != 0 && $self->{$key} != 1)) { |
|
|
|
100
|
|
|
|
|
75
|
|
|
|
|
|
|
err "Parameter '$key' must be a bool (0/1).", |
76
|
2
|
|
|
|
|
14
|
'Value', $self->{$key}, |
77
|
|
|
|
|
|
|
; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
|
|
7
|
return; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub check_code { |
84
|
4
|
|
|
4
|
1
|
3429
|
my ($self, $key) = @_; |
85
|
|
|
|
|
|
|
|
86
|
4
|
100
|
|
|
|
9
|
_check_key($self, $key) && return; |
87
|
|
|
|
|
|
|
|
88
|
2
|
100
|
|
|
|
10
|
if (ref $self->{$key} ne 'CODE') { |
89
|
|
|
|
|
|
|
err "Parameter '$key' must be a code.", |
90
|
1
|
|
|
|
|
7
|
'Value', $self->{$key}, |
91
|
|
|
|
|
|
|
; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
|
|
3
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub check_isa { |
98
|
7
|
|
|
7
|
1
|
6974
|
my ($self, $key, $class) = @_; |
99
|
|
|
|
|
|
|
|
100
|
7
|
100
|
|
|
|
16
|
_check_key($self, $key) && return; |
101
|
|
|
|
|
|
|
|
102
|
5
|
100
|
|
|
|
18
|
if (! blessed($self->{$key})) { |
103
|
|
|
|
|
|
|
err "Parameter '$key' must be a '$class' object.", |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Only, if value is scalar. |
106
|
|
|
|
|
|
|
(ref $self->{$key} eq '') ? ( |
107
|
|
|
|
|
|
|
'Value', $self->{$key}, |
108
|
|
|
|
|
|
|
) : (), |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Only if value is reference. |
111
|
|
|
|
|
|
|
(ref $self->{$key} ne '') ? ( |
112
|
3
|
100
|
|
|
|
26
|
'Reference', (ref $self->{$key}), |
|
|
100
|
|
|
|
|
|
113
|
|
|
|
|
|
|
) : (), |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
2
|
100
|
|
|
|
20
|
if (! $self->{$key}->isa($class)) { |
117
|
|
|
|
|
|
|
err "Parameter '$key' must be a '$class' object.", |
118
|
1
|
|
|
|
|
10
|
'Reference', (ref $self->{$key}), |
119
|
|
|
|
|
|
|
; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
3
|
return; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub check_length { |
126
|
5
|
|
|
5
|
1
|
3998
|
my ($self, $key, $max_length) = @_; |
127
|
|
|
|
|
|
|
|
128
|
5
|
100
|
|
|
|
13
|
_check_key($self, $key) && return; |
129
|
|
|
|
|
|
|
|
130
|
3
|
100
|
|
|
|
9
|
if (length $self->{$key} > $max_length) { |
131
|
|
|
|
|
|
|
err "Parameter '$key' has length greater than '$max_length'.", |
132
|
1
|
|
|
|
|
7
|
'Value', $self->{$key}, |
133
|
|
|
|
|
|
|
; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
2
|
|
|
|
|
5
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub check_number { |
140
|
7
|
|
|
7
|
1
|
4396
|
my ($self, $key) = @_; |
141
|
|
|
|
|
|
|
|
142
|
7
|
100
|
|
|
|
19
|
_check_key($self, $key) && return; |
143
|
|
|
|
|
|
|
|
144
|
5
|
100
|
|
|
|
53
|
if ($self->{$key} !~ m/^[-+]?\d+(\.\d+)?$/ms) { |
145
|
|
|
|
|
|
|
err "Parameter '$key' must be a number.", |
146
|
1
|
|
|
|
|
6
|
'Value', $self->{$key}, |
147
|
|
|
|
|
|
|
; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
4
|
|
|
|
|
12
|
return; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub check_number_of_items { |
154
|
2
|
|
|
2
|
1
|
3357
|
my ($self, $list_method, $item_method, $object_name, $item_name) = @_; |
155
|
|
|
|
|
|
|
|
156
|
2
|
|
|
|
|
4
|
my $item_hr = {}; |
157
|
2
|
|
|
|
|
4
|
foreach my $item (@{$self->$list_method}) { |
|
2
|
|
|
|
|
14
|
|
158
|
4
|
|
|
|
|
301
|
$item_hr->{$item->$item_method} += 1; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
2
|
|
|
|
|
114
|
foreach my $item (keys %{$item_hr}) { |
|
2
|
|
|
|
|
8
|
|
162
|
3
|
100
|
|
|
|
8
|
if ($item_hr->{$item} > 1) { |
163
|
1
|
|
|
|
|
8
|
err "$object_name for $item_name '$item' has multiple values." |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
5
|
return; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub check_regexp { |
171
|
4
|
|
|
4
|
1
|
3489
|
my ($self, $key, $regexp) = @_; |
172
|
|
|
|
|
|
|
|
173
|
4
|
100
|
|
|
|
10
|
_check_key($self, $key) && return; |
174
|
|
|
|
|
|
|
|
175
|
3
|
100
|
|
|
|
8
|
if (! defined $regexp) { |
176
|
1
|
|
|
|
|
5
|
err "Parameter '$key' must have defined regexp."; |
177
|
|
|
|
|
|
|
} |
178
|
2
|
100
|
|
|
|
33
|
if ($self->{$key} !~ m/^$regexp/ms) { |
179
|
|
|
|
|
|
|
err "Parameter '$key' does not match the specified regular expression.", |
180
|
1
|
|
|
|
|
7
|
'String', $self->{$key}, |
181
|
|
|
|
|
|
|
'Regexp', $regexp, |
182
|
|
|
|
|
|
|
; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
1
|
|
|
|
|
4
|
return; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub check_required { |
189
|
3
|
|
|
3
|
1
|
3729
|
my ($self, $key) = @_; |
190
|
|
|
|
|
|
|
|
191
|
3
|
100
|
100
|
|
|
19
|
if (! exists $self->{$key} || ! defined $self->{$key}) { |
192
|
2
|
|
|
|
|
9
|
err "Parameter '$key' is required."; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
1
|
|
|
|
|
3
|
return; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub check_string_begin { |
199
|
4
|
|
|
4
|
1
|
3348
|
my ($self, $key, $string_base) = @_; |
200
|
|
|
|
|
|
|
|
201
|
4
|
100
|
|
|
|
11
|
_check_key($self, $key) && return; |
202
|
|
|
|
|
|
|
|
203
|
3
|
100
|
|
|
|
8
|
if (! defined $string_base) { |
204
|
1
|
|
|
|
|
7
|
err "Parameter '$key' must have defined string base."; |
205
|
|
|
|
|
|
|
} |
206
|
2
|
100
|
|
|
|
23
|
if ($self->{$key} !~ m/^$string_base/) { |
207
|
|
|
|
|
|
|
err "Parameter '$key' must begin with defined string base.", |
208
|
1
|
|
|
|
|
7
|
'String', $self->{$key}, |
209
|
|
|
|
|
|
|
'String base', $string_base, |
210
|
|
|
|
|
|
|
; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
1
|
|
|
|
|
4
|
return; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub check_strings { |
217
|
6
|
|
|
6
|
1
|
4868
|
my ($self, $key, $strings_ar) = @_; |
218
|
|
|
|
|
|
|
|
219
|
6
|
100
|
|
|
|
14
|
_check_key($self, $key) && return; |
220
|
|
|
|
|
|
|
|
221
|
5
|
100
|
|
|
|
12
|
if (! defined $strings_ar) { |
222
|
1
|
|
|
|
|
9
|
err "Parameter '$key' must have strings definition."; |
223
|
|
|
|
|
|
|
} |
224
|
4
|
100
|
|
|
|
11
|
if (ref $strings_ar ne 'ARRAY') { |
225
|
1
|
|
|
|
|
5
|
err "Parameter '$key' must have right string definition."; |
226
|
|
|
|
|
|
|
} |
227
|
3
|
100
|
|
5
|
|
12
|
if (none { $self->{$key} eq $_ } @{$strings_ar}) { |
|
5
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
12
|
|
228
|
|
|
|
|
|
|
err "Parameter '$key' must be one of defined strings.", |
229
|
|
|
|
|
|
|
'String', $self->{$key}, |
230
|
1
|
|
|
|
|
5
|
'Possible strings', (join ', ', @{$strings_ar}), |
|
1
|
|
|
|
|
5
|
|
231
|
|
|
|
|
|
|
; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
2
|
|
|
|
|
16
|
return; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub _check_key { |
238
|
43
|
|
|
43
|
|
108
|
my ($self, $key) = @_; |
239
|
|
|
|
|
|
|
|
240
|
43
|
100
|
100
|
|
|
250
|
if (! exists $self->{$key} || ! defined $self->{$key}) { |
241
|
13
|
|
|
|
|
61
|
return 1; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
30
|
|
|
|
|
101
|
return 0; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
__END__ |