| 1 | package Hash::Sanitize; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Clone; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | 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 | 2 | 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 | 0 | ▶ | sub sanitize_hash { | ||||||||||||||||||||||||||
|
Subroutine
sanitize_hash: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($hash,$allowed_keys) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | die "First argument of sanitize_hash must be a HASH REF!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $hash and ref $hash eq "HASH"
MC/DC0%0/2
ref $hash and ref $hash eq "HASH"
ref $hash
ref $hash eq "HASH"
Truth table0%0/3
A and B
A= ref $hashB= ref $hash eq "HASH"
| |||||||||||||||||||||||||||||
| 91 | unless ref($hash) && ref($hash) eq "HASH"; | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | die "Second argument of sanitize_hash must be a ARRAY REF!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $allowed_keys and ref $allowed_keys eq "ARRAY"
MC/DC0%0/2
ref $allowed_keys and ref $allowed_keys eq "ARRAY"
ref $allowed_keys
ref $allowed_keys eq "ARRAY"
Truth table0%0/3
A and B
A= ref $allowed_keysB= ref $allowed_keys eq "ARRAY"
| |||||||||||||||||||||||||||||
| 93 | unless ref($allowed_keys) && ref($allowed_keys) eq "ARRAY"; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # not in void context | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | return %{$copy}; | |||||||||||||||||||||||||||
| 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}) { | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | delete $hash->{$k} unless ( grep { $k eq $_ } @{$allowed_keys} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub sanitize_hash_deep { | ||||||||||||||||||||||||||
|
Subroutine
sanitize_hash_deep: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($hash,$allowed_keys) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | die "First argument of sanitize_hash_deep must be a HASH REF!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $hash and ref $hash eq "HASH"
MC/DC0%0/2
ref $hash and ref $hash eq "HASH"
ref $hash
ref $hash eq "HASH"
Truth table0%0/3
A and B
A= ref $hashB= ref $hash eq "HASH"
| |||||||||||||||||||||||||||||
| 150 | unless ref($hash) && ref($hash) eq "HASH"; | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | die "Second argument of sanitize_hash_deep must be a ARRAY REF!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $allowed_keys and ref $allowed_keys eq "ARRAY"
MC/DC0%0/2
ref $allowed_keys and ref $allowed_keys eq "ARRAY"
ref $allowed_keys
ref $allowed_keys eq "ARRAY"
Truth table0%0/3
A and B
A= ref $allowed_keysB= ref $allowed_keys eq "ARRAY"
| |||||||||||||||||||||||||||||
| 152 | unless ref($allowed_keys) && ref($allowed_keys) eq "ARRAY"; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # not in void context | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | return %{$copy}; | |||||||||||||||||||||||||||
| 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}) { | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (! (grep { $k eq $_ } @{$allowed_keys}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | delete $hash->{$k}; | |||||||||||||||||||||||||||
| 175 | 0 | next; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if (ref($hash->{$k}) && ref($hash->{$k}) eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $hash->{$k} and ref $hash->{$k} eq "HASH"
MC/DC0%0/2
ref $hash->{$k} and ref $hash->{$k} eq "HASH"
ref $hash->{$k}
ref $hash->{$k} eq "HASH"
Truth table0%0/3
A and B
A= ref $hash->{$k}B= 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<< <gliganh at gmail.com> >> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 BUGS | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Please report any bugs or feature requests to C<bug-hash-sanitize at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 193 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HASH-Sanitize>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HASH-Sanitize> | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | L<http://annocpan.org/dist/HASH-Sanitize> | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | L<http://cpanratings.perl.org/d/HASH-Sanitize> | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | L<http://search.cpan.org/dist/HASH-Sanitize/> | ||||||||||||||||||||||||||||
| 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 | 2 | 1; # End of Hash::Sanitize | |||||||||||||||||||||||||||