| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Hash::Sanitize; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13330
|
use 5.010; |
|
|
1
|
|
|
|
|
2
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
18
|
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings FATAL => 'all'; |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
40
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
3
|
use base 'Exporter'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
70
|
|
|
8
|
1
|
|
|
1
|
|
399
|
use Clone; |
|
|
1
|
|
|
|
|
1965
|
|
|
|
1
|
|
|
|
|
310
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = (qw/sanitize_hash sanitize_hash_deep/); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Hash::Sanitize - Remove undesired keys from a hash (recursive) |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 VERSION |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Version 0.04 |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This module implements two methods that allows you to clean up a hash of |
|
27
|
|
|
|
|
|
|
undesired keys. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
When called the method will iterate trough the hash keys and delete any keys |
|
30
|
|
|
|
|
|
|
that are non in the desired set. |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This module is like Hash::Util's "legal_keys" method with the difference that |
|
33
|
|
|
|
|
|
|
while legal_keys doens't let you create keys that are not allowed, this module |
|
34
|
|
|
|
|
|
|
alows you to modify an existing hash and get a sanitized copy of it. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Quick summary of what the module does. |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Perhaps a little code snippet. |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
use Hash::Sanitize qw(sanitize_hash sanitize_hash_deep); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sanitize_hash(\%hash,\@allowed_keys); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sanitize_hash_deep(\%hash,\@allowed_keys); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 EXPORT |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
This module exports two methods: |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sanitize_hash |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
and |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sanitize_hash_deep |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 sanitize_hash |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Given a hash, it iterates trough the list of keys in the hash and deletes |
|
63
|
|
|
|
|
|
|
the keys that are not in the allowed keys list |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
When called in a void context, it modifies the hash sent as a parameter. |
|
66
|
|
|
|
|
|
|
When called in a array context (hash to be more exact) it will clone the |
|
67
|
|
|
|
|
|
|
original hash and sanitize & return the copy, leaving the original hash intact. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Example : |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my %new_hash = sanitize_hash(\%hash,[qw/foo bar/]); |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Called this way the method leaves %hash intact and returns a hash containing |
|
74
|
|
|
|
|
|
|
only "foo" and "bar" keys (if they exist) |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Example 2: |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sanitize_hash(\%hash,[qw/foo bar/]); |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Called this way the method remove all the heys from %hash that are not "foo" |
|
81
|
|
|
|
|
|
|
or "bar". Called this way the method *will* modifiy the setucture of the |
|
82
|
|
|
|
|
|
|
original hash that was passed as an argument |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
In scalar context will return a hash referece, to a copy of the original hash. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
|
87
|
|
|
|
|
|
|
sub sanitize_hash { |
|
88
|
0
|
|
|
0
|
1
|
|
my ($hash,$allowed_keys) = @_; |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
0
|
0
|
|
|
|
die "First argument of sanitize_hash must be a HASH REF!" |
|
91
|
|
|
|
|
|
|
unless ref($hash) && ref($hash) eq "HASH"; |
|
92
|
0
|
0
|
0
|
|
|
|
die "Second argument of sanitize_hash must be a ARRAY REF!" |
|
93
|
|
|
|
|
|
|
unless ref($allowed_keys) && ref($allowed_keys) eq "ARRAY"; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# not in void context |
|
96
|
0
|
0
|
|
|
|
|
if (defined wantarray) { |
|
97
|
|
|
|
|
|
|
#make a copy |
|
98
|
0
|
|
|
|
|
|
my $copy = Clone::clone($hash); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#sanitize the copy |
|
101
|
0
|
|
|
|
|
|
sanitize_hash($copy,$allowed_keys); |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#return |
|
104
|
0
|
0
|
|
|
|
|
if (wantarray) { |
|
105
|
0
|
|
|
|
|
|
return %{$copy}; |
|
|
0
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
else { |
|
108
|
0
|
|
|
|
|
|
return $copy; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
else { #void context |
|
112
|
|
|
|
|
|
|
#delete the keys that are not allowed to be there |
|
113
|
0
|
|
|
|
|
|
foreach my $k (keys %{$hash}) { |
|
|
0
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
delete $hash->{$k} unless ( grep { $k eq $_ } @{$allowed_keys} ); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 sanitize_hash_deep |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Same as sanitize_hash but this method will also sanitize the HASH structures |
|
122
|
|
|
|
|
|
|
that are found as values for allowed keys |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Example : |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my %hash = ( |
|
127
|
|
|
|
|
|
|
a => 1, |
|
128
|
|
|
|
|
|
|
b => 2, |
|
129
|
|
|
|
|
|
|
c => { d => 3, e => 4, f => 5}, |
|
130
|
|
|
|
|
|
|
g => 6, |
|
131
|
|
|
|
|
|
|
); |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my %hash_copy = sanitize_hash_deep(\%hash,[qw/a c d/]); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
The content of %hash_copy will be : |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
( a => 1, c => { d => 3 } ) |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
It can also be called in a void context. In this case it will apply all changes |
|
140
|
|
|
|
|
|
|
to the original hash that was passed as an argument |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
In scalar context will return a hash referece, to a copy of the original hash. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub sanitize_hash_deep { |
|
147
|
0
|
|
|
0
|
1
|
|
my ($hash,$allowed_keys) = @_; |
|
148
|
|
|
|
|
|
|
|
|
149
|
0
|
0
|
0
|
|
|
|
die "First argument of sanitize_hash_deep must be a HASH REF!" |
|
150
|
|
|
|
|
|
|
unless ref($hash) && ref($hash) eq "HASH"; |
|
151
|
0
|
0
|
0
|
|
|
|
die "Second argument of sanitize_hash_deep must be a ARRAY REF!" |
|
152
|
|
|
|
|
|
|
unless ref($allowed_keys) && ref($allowed_keys) eq "ARRAY"; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# not in void context |
|
155
|
0
|
0
|
|
|
|
|
if (defined wantarray) { |
|
156
|
|
|
|
|
|
|
#make a copy |
|
157
|
0
|
|
|
|
|
|
my $copy = Clone::clone($hash); |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
#sanitize the copy |
|
160
|
0
|
|
|
|
|
|
sanitize_hash_deep($copy,$allowed_keys); |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
#return |
|
163
|
0
|
0
|
|
|
|
|
if (wantarray) { |
|
164
|
0
|
|
|
|
|
|
return %{$copy}; |
|
|
0
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
else { |
|
167
|
0
|
|
|
|
|
|
return $copy; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
else { #void context |
|
171
|
|
|
|
|
|
|
#delete the keys that are not allowed to be there |
|
172
|
0
|
|
|
|
|
|
foreach my $k (keys %{$hash}) { |
|
|
0
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
|
if (! (grep { $k eq $_ } @{$allowed_keys}) ) { |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
delete $hash->{$k}; |
|
175
|
0
|
|
|
|
|
|
next; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
else { |
|
178
|
0
|
0
|
0
|
|
|
|
if (ref($hash->{$k}) && ref($hash->{$k}) eq "HASH") { |
|
179
|
0
|
|
|
|
|
|
sanitize_hash_deep($hash->{$k},$allowed_keys); |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Horea Gligan, C<< >> |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 BUGS |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
193
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
194
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 SUPPORT |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
perldoc Hash::Sanitize |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
You can also look for information at: |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=over 4 |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
L |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
L |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
L |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * Search CPAN |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
L |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=back |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Copyright 2012 Evozon Systems |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
235
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
236
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
1; # End of Hash::Sanitize |