line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mo::utils; |
2
|
|
|
|
|
|
|
|
3
|
13
|
|
|
13
|
|
208086
|
use base qw(Exporter); |
|
13
|
|
|
|
|
113
|
|
|
13
|
|
|
|
|
1441
|
|
4
|
13
|
|
|
13
|
|
106
|
use strict; |
|
13
|
|
|
|
|
24
|
|
|
13
|
|
|
|
|
325
|
|
5
|
13
|
|
|
13
|
|
60
|
use warnings; |
|
13
|
|
|
|
|
41
|
|
|
13
|
|
|
|
|
487
|
|
6
|
|
|
|
|
|
|
|
7
|
13
|
|
|
13
|
|
5946
|
use Error::Pure qw(err); |
|
13
|
|
|
|
|
34686
|
|
|
13
|
|
|
|
|
246
|
|
8
|
13
|
|
|
13
|
|
889
|
use List::Util qw(none); |
|
13
|
|
|
|
|
31
|
|
|
13
|
|
|
|
|
1789
|
|
9
|
13
|
|
|
13
|
|
90
|
use Readonly; |
|
13
|
|
|
|
|
28
|
|
|
13
|
|
|
|
|
709
|
|
10
|
13
|
|
|
13
|
|
121
|
use Scalar::Util qw(blessed); |
|
13
|
|
|
|
|
29
|
|
|
13
|
|
|
|
|
26160
|
|
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_required check_string_begin check_strings); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = 0.16; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub check_array { |
19
|
8
|
|
|
8
|
1
|
3473
|
my ($self, $key) = @_; |
20
|
|
|
|
|
|
|
|
21
|
8
|
100
|
|
|
|
25
|
if (! exists $self->{$key}) { |
22
|
2
|
|
|
|
|
7
|
return; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
6
|
100
|
|
|
|
22
|
if (ref $self->{$key} ne 'ARRAY') { |
26
|
|
|
|
|
|
|
err "Parameter '".$key."' must be a array.", |
27
|
|
|
|
|
|
|
'Value', $self->{$key}, |
28
|
3
|
|
|
|
|
20
|
'Reference', (ref $self->{$key}), |
29
|
|
|
|
|
|
|
; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
6
|
return; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub check_array_object { |
36
|
4
|
|
|
4
|
1
|
4383
|
my ($self, $key, $class, $class_name) = @_; |
37
|
|
|
|
|
|
|
|
38
|
4
|
|
|
|
|
10
|
check_array($self, $key); |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
|
|
5
|
foreach my $obj (@{$self->{$key}}) { |
|
3
|
|
|
|
|
9
|
|
41
|
2
|
100
|
|
|
|
16
|
if (! $obj->isa($class)) { |
42
|
1
|
|
|
|
|
5
|
err $class_name." isn't '".$class."' object."; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
16
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub check_bool { |
50
|
6
|
|
|
6
|
1
|
5100
|
my ($self, $key) = @_; |
51
|
|
|
|
|
|
|
|
52
|
6
|
100
|
|
|
|
14
|
_check_key($self, $key) && return; |
53
|
|
|
|
|
|
|
|
54
|
4
|
100
|
100
|
|
|
36
|
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
|
|
|
|
|
13
|
'Value', $self->{$key}, |
57
|
|
|
|
|
|
|
; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
2
|
|
|
|
|
5
|
return; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub check_code { |
64
|
4
|
|
|
4
|
1
|
3451
|
my ($self, $key) = @_; |
65
|
|
|
|
|
|
|
|
66
|
4
|
100
|
|
|
|
13
|
_check_key($self, $key) && return; |
67
|
|
|
|
|
|
|
|
68
|
2
|
100
|
|
|
|
8
|
if (ref $self->{$key} ne 'CODE') { |
69
|
|
|
|
|
|
|
err "Parameter '$key' must be a code.", |
70
|
1
|
|
|
|
|
6
|
'Value', $self->{$key}, |
71
|
|
|
|
|
|
|
; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
2
|
return; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub check_isa { |
78
|
7
|
|
|
7
|
1
|
7161
|
my ($self, $key, $class) = @_; |
79
|
|
|
|
|
|
|
|
80
|
7
|
100
|
|
|
|
19
|
_check_key($self, $key) && return; |
81
|
|
|
|
|
|
|
|
82
|
5
|
100
|
|
|
|
18
|
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
|
|
|
|
18
|
if (! $self->{$key}->isa($class)) { |
97
|
|
|
|
|
|
|
err "Parameter '$key' must be a '$class' object.", |
98
|
1
|
|
|
|
|
8
|
'Reference', (ref $self->{$key}), |
99
|
|
|
|
|
|
|
; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
1
|
|
|
|
|
3
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub check_length { |
106
|
5
|
|
|
5
|
1
|
3892
|
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
|
|
|
|
|
8
|
'Value', $self->{$key}, |
113
|
|
|
|
|
|
|
; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
|
|
5
|
return; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub check_number { |
120
|
7
|
|
|
7
|
1
|
4565
|
my ($self, $key) = @_; |
121
|
|
|
|
|
|
|
|
122
|
7
|
100
|
|
|
|
17
|
_check_key($self, $key) && return; |
123
|
|
|
|
|
|
|
|
124
|
5
|
100
|
|
|
|
42
|
if ($self->{$key} !~ m/^[-+]?\d+(\.\d+)?$/ms) { |
125
|
|
|
|
|
|
|
err "Parameter '$key' must be a number.", |
126
|
1
|
|
|
|
|
7
|
'Value', $self->{$key}, |
127
|
|
|
|
|
|
|
; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
4
|
|
|
|
|
9
|
return; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub check_number_of_items { |
134
|
2
|
|
|
2
|
1
|
3186
|
my ($self, $list_method, $item_method, $object_name, $item_name) = @_; |
135
|
|
|
|
|
|
|
|
136
|
2
|
|
|
|
|
5
|
my $item_hr = {}; |
137
|
2
|
|
|
|
|
4
|
foreach my $item (@{$self->$list_method}) { |
|
2
|
|
|
|
|
15
|
|
138
|
4
|
|
|
|
|
286
|
$item_hr->{$item->$item_method} += 1; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
2
|
|
|
|
|
160
|
foreach my $item (keys %{$item_hr}) { |
|
2
|
|
|
|
|
7
|
|
142
|
3
|
100
|
|
|
|
11
|
if ($item_hr->{$item} > 1) { |
143
|
1
|
|
|
|
|
9
|
err "$object_name for $item_name '$item' has multiple values." |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
|
|
7
|
return; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub check_required { |
151
|
3
|
|
|
3
|
1
|
4209
|
my ($self, $key) = @_; |
152
|
|
|
|
|
|
|
|
153
|
3
|
100
|
100
|
|
|
19
|
if (! exists $self->{$key} || ! defined $self->{$key}) { |
154
|
2
|
|
|
|
|
13
|
err "Parameter '$key' is required."; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
1
|
|
|
|
|
4
|
return; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub check_string_begin { |
161
|
4
|
|
|
4
|
1
|
3493
|
my ($self, $key, $string_base) = @_; |
162
|
|
|
|
|
|
|
|
163
|
4
|
100
|
|
|
|
10
|
_check_key($self, $key) && return; |
164
|
|
|
|
|
|
|
|
165
|
3
|
100
|
|
|
|
9
|
if (! defined $string_base) { |
166
|
1
|
|
|
|
|
15
|
err "Parameter '$key' must have defined string base."; |
167
|
|
|
|
|
|
|
} |
168
|
2
|
100
|
|
|
|
30
|
if ($self->{$key} !~ m/^$string_base/) { |
169
|
|
|
|
|
|
|
err "Parameter '$key' must begin with defined string base.", |
170
|
1
|
|
|
|
|
11
|
'String', $self->{$key}, |
171
|
|
|
|
|
|
|
'String base', $string_base, |
172
|
|
|
|
|
|
|
; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
1
|
|
|
|
|
4
|
return; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub check_strings { |
179
|
6
|
|
|
6
|
1
|
5110
|
my ($self, $key, $strings_ar) = @_; |
180
|
|
|
|
|
|
|
|
181
|
6
|
100
|
|
|
|
16
|
_check_key($self, $key) && return; |
182
|
|
|
|
|
|
|
|
183
|
5
|
100
|
|
|
|
16
|
if (! defined $strings_ar) { |
184
|
1
|
|
|
|
|
8
|
err "Parameter '$key' must have strings definition."; |
185
|
|
|
|
|
|
|
} |
186
|
4
|
100
|
|
|
|
13
|
if (ref $strings_ar ne 'ARRAY') { |
187
|
1
|
|
|
|
|
7
|
err "Parameter '$key' must have right string definition."; |
188
|
|
|
|
|
|
|
} |
189
|
3
|
100
|
|
5
|
|
14
|
if (none { $self->{$key} eq $_ } @{$strings_ar}) { |
|
5
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
13
|
|
190
|
|
|
|
|
|
|
err "Parameter '$key' must be one of defined strings.", |
191
|
|
|
|
|
|
|
'String', $self->{$key}, |
192
|
1
|
|
|
|
|
6
|
'Possible strings', (join ', ', @{$strings_ar}), |
|
1
|
|
|
|
|
7
|
|
193
|
|
|
|
|
|
|
; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
2
|
|
|
|
|
17
|
return; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub _check_key { |
200
|
39
|
|
|
39
|
|
76
|
my ($self, $key) = @_; |
201
|
|
|
|
|
|
|
|
202
|
39
|
100
|
100
|
|
|
210
|
if (! exists $self->{$key} || ! defined $self->{$key}) { |
203
|
12
|
|
|
|
|
60
|
return 1; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
27
|
|
|
|
|
76
|
return 0; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
1; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
__END__ |