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