line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FormValidator::LazyWay::Utils; |
2
|
|
|
|
|
|
|
|
3
|
30
|
|
|
30
|
|
146224
|
use strict; |
|
30
|
|
|
|
|
58
|
|
|
30
|
|
|
|
|
1023
|
|
4
|
30
|
|
|
30
|
|
148
|
use warnings; |
|
30
|
|
|
|
|
48
|
|
|
30
|
|
|
|
|
738
|
|
5
|
30
|
|
|
30
|
|
172
|
use Scalar::Util; |
|
30
|
|
|
|
|
50
|
|
|
30
|
|
|
|
|
2338
|
|
6
|
30
|
|
|
30
|
|
32076
|
use Perl6::Junction qw/any/; |
|
30
|
|
|
|
|
366986
|
|
|
30
|
|
|
|
|
26835
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub check_profile_syntax { |
9
|
42
|
|
|
42
|
0
|
150
|
my $profile = shift; |
10
|
|
|
|
|
|
|
|
11
|
42
|
50
|
|
|
|
194
|
( ref $profile eq 'HASH' ) |
12
|
|
|
|
|
|
|
or die "Invalid input profile: needs to be a hash reference\n"; |
13
|
|
|
|
|
|
|
|
14
|
42
|
|
|
|
|
156
|
my @invalid; |
15
|
|
|
|
|
|
|
{ |
16
|
42
|
|
|
|
|
154
|
my @valid_profile_keys = ( |
|
42
|
|
|
|
|
254
|
|
17
|
|
|
|
|
|
|
qw/ |
18
|
|
|
|
|
|
|
required |
19
|
|
|
|
|
|
|
optional |
20
|
|
|
|
|
|
|
defaults |
21
|
|
|
|
|
|
|
want_array |
22
|
|
|
|
|
|
|
stash |
23
|
|
|
|
|
|
|
lang |
24
|
|
|
|
|
|
|
level |
25
|
|
|
|
|
|
|
dependency_groups |
26
|
|
|
|
|
|
|
dependencies |
27
|
|
|
|
|
|
|
use_fixed_method |
28
|
|
|
|
|
|
|
/ |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
42
|
|
|
|
|
160
|
for my $key ( keys %$profile ) { |
32
|
65
|
100
|
|
|
|
1360
|
next if $key =~ m/^use_/; |
33
|
62
|
100
|
|
|
|
275
|
push @invalid, $key unless ( $key eq any(@valid_profile_keys) ); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
42
|
|
|
|
|
2683
|
local $" = ', '; |
37
|
42
|
100
|
|
|
|
191
|
if (@invalid) { |
38
|
1
|
|
|
|
|
12
|
die "Invalid input profile: keys not recognised [@invalid]\n"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
41
|
|
|
|
|
121
|
return 1; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub remove_empty_fields { |
46
|
43
|
|
|
43
|
0
|
191
|
my $valid = shift; |
47
|
|
|
|
|
|
|
|
48
|
43
|
|
|
|
|
115
|
for my $field ( keys %{$valid} ) { |
|
43
|
|
|
|
|
157
|
|
49
|
80
|
100
|
|
|
|
303
|
if ( ref $valid->{$field} ) { |
50
|
1
|
50
|
|
|
|
5
|
next if ref $valid->{$field} ne 'ARRAY'; |
51
|
1
|
|
|
|
|
3
|
for ( my $i = 0; $i < scalar @{ $valid->{$field} }; $i++ ) { |
|
6
|
|
|
|
|
16
|
|
52
|
5
|
50
|
66
|
|
|
31
|
$valid->{$field}->[$i] = undef |
|
|
|
33
|
|
|
|
|
53
|
|
|
|
|
|
|
unless ( defined $valid->{$field}->[$i] |
54
|
|
|
|
|
|
|
and length $valid->{$field}->[$i] |
55
|
|
|
|
|
|
|
and $valid->{$field}->[$i] !~ /^\x00$/ ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
4
|
my @tmp_valid = (); |
59
|
1
|
|
|
|
|
1
|
for my $item( @{ $valid->{$field} } ) { |
|
1
|
|
|
|
|
4
|
|
60
|
5
|
50
|
|
|
|
12
|
push @tmp_valid , $item if defined $item; |
61
|
|
|
|
|
|
|
} |
62
|
1
|
|
|
|
|
3
|
$valid->{$field} = \@tmp_valid; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# If all fields are empty, we delete it. |
65
|
0
|
|
|
|
|
0
|
delete $valid->{$field} |
66
|
1
|
50
|
|
|
|
3
|
unless grep { defined $_ } @{ $valid->{$field} }; |
|
1
|
|
|
|
|
6
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
79
|
100
|
100
|
|
|
846
|
delete $valid->{$field} |
|
|
|
66
|
|
|
|
|
70
|
|
|
|
|
|
|
unless ( defined $valid->{$field} |
71
|
|
|
|
|
|
|
and length $valid->{$field} |
72
|
|
|
|
|
|
|
and $valid->{$field} !~ /^\x00$/ ); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
43
|
|
|
|
|
183
|
$valid; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub arrayify { |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# if the input is undefined, return an empty list |
82
|
145
|
|
|
145
|
0
|
295
|
my $val = shift; |
83
|
145
|
100
|
|
|
|
531
|
defined $val or return (); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# if it's a reference, return an array unless it points to an empty array. -mls |
86
|
64
|
100
|
|
|
|
215
|
if ( ref $val eq 'ARRAY' ) { |
87
|
63
|
|
|
|
|
162
|
$^W = 0; # turn off warnings about undef |
88
|
63
|
100
|
|
|
|
243
|
return ( any(@$val) ne undef ) ? @$val : (); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# if it's a string, return an array unless the string is missing or empty. -mls |
92
|
|
|
|
|
|
|
else { |
93
|
1
|
50
|
|
|
|
6
|
return ( length $val ) ? ($val) : (); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Figure out whether the data is a hash reference of a param-capable object and return it has a hash |
98
|
|
|
|
|
|
|
sub get_input_as_hash { |
99
|
41
|
|
|
41
|
0
|
4971
|
my $data = shift; |
100
|
41
|
|
|
|
|
352
|
require Scalar::Util; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# This checks whether we have an object that supports param |
103
|
41
|
100
|
66
|
|
|
716
|
if ( Scalar::Util::blessed($data) && $data->can('param') ) { |
|
|
50
|
|
|
|
|
|
104
|
40
|
|
|
|
|
943
|
my %return; |
105
|
40
|
|
|
|
|
190
|
for my $k ( $data->param() ) { |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# we expect param to return an array if there are multiple values |
108
|
73
|
|
|
|
|
1169
|
my @v; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# CGI::Simple requires us to call 'upload()' to get upload data, |
111
|
|
|
|
|
|
|
# while CGI/Apache::Request return it on calling 'param()'. |
112
|
|
|
|
|
|
|
# |
113
|
|
|
|
|
|
|
# This seems quirky, but there isn't a way for us to easily check if |
114
|
|
|
|
|
|
|
# "this field contains a file upload" or not. |
115
|
73
|
50
|
|
|
|
522
|
if ( $data->isa('CGI::Simple') ) { |
116
|
0
|
|
0
|
|
|
0
|
@v = $data->upload($k) || $data->param($k); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
else { |
119
|
73
|
|
|
|
|
563
|
@v = $data->param($k); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# we expect param to return an array if there are multiple values |
123
|
73
|
100
|
|
|
|
1824
|
$return{$k} = scalar(@v) > 1 ? \@v : $v[0]; |
124
|
|
|
|
|
|
|
} |
125
|
40
|
|
|
|
|
406
|
return \%return; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# otherwise, it's already a hash reference |
129
|
|
|
|
|
|
|
elsif ( ref $data eq 'HASH' ) { |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# be careful to actually copy array references |
132
|
1
|
|
|
|
|
4
|
my %copy = %$data; |
133
|
1
|
|
|
|
|
4
|
for ( grep { ref $data->{$_} eq 'ARRAY' } keys %$data ) { |
|
1
|
|
|
|
|
6
|
|
134
|
0
|
|
|
|
|
0
|
my @array_copy = @{ $data->{$_} }; |
|
0
|
|
|
|
|
0
|
|
135
|
0
|
|
|
|
|
0
|
$copy{$_} = \@array_copy; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
20
|
return \%copy; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
else { |
141
|
0
|
|
|
|
|
|
die |
142
|
|
|
|
|
|
|
"FormValidator::LazyWay->validate() or check() called with invalid input data structure."; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 NAME |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
FormValidator::LazyWay::Util - FormValidator::LazyWay Util functions |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 AUTHOR |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Tomohiro Teranishi |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |