Hash::Sanitize - Remove undesired keys from a hash (recursive)
Version 0.04
This module implements two methods that allows you to clean up a hash of undesired keys.
When called the method will iterate trough the hash keys and delete any keys that are non in the desired set.
This module is like Hash::Util's "legal_keys" method with the difference that while legal_keys doens't let you create keys that are not allowed, this module alows you to modify an existing hash and get a sanitized copy of it.
Quick summary of what the module does.
Perhaps a little code snippet.
use Hash::Sanitize qw(sanitize_hash sanitize_hash_deep); sanitize_hash(\%hash,\@allowed_keys); sanitize_hash_deep(\%hash,\@allowed_keys);
This module exports two methods:
sanitize_hash and sanitize_hash_deep
Given a hash, it iterates trough the list of keys in the hash and deletes the keys that are not in the allowed keys list
When called in a void context, it modifies the hash sent as a parameter. When called in a array context (hash to be more exact) it will clone the original hash and sanitize & return the copy, leaving the original hash intact.
Example :
my %new_hash = sanitize_hash(\%hash,[qw/foo bar/]);
Called this way the method leaves %hash intact and returns a hash containing only "foo" and "bar" keys (if they exist)
Example 2:
sanitize_hash(\%hash,[qw/foo bar/]);
Called this way the method remove all the heys from %hash that are not "foo" or "bar". Called this way the method *will* modifiy the setucture of the original hash that was passed as an argument
In scalar context will return a hash referece, to a copy of the original hash.
Same as sanitize_hash but this method will also sanitize the HASH structures that are found as values for allowed keys
my %hash = ( a => 1, b => 2, c => { d => 3, e => 4, f => 5}, g => 6, ); my %hash_copy = sanitize_hash_deep(\%hash,[qw/a c d/]);
The content of %hash_copy will be :
( a => 1, c => { d => 3 } )
It can also be called in a void context. In this case it will apply all changes to the original hash that was passed as an argument
Horea Gligan, <gliganh at gmail.com>
<gliganh at gmail.com>
Please report any bugs or feature requests to bug-hash-sanitize at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HASH-Sanitize. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-hash-sanitize at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Hash::Sanitize
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=HASH-Sanitize
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/HASH-Sanitize
CPAN Ratings
http://cpanratings.perl.org/d/HASH-Sanitize
Search CPAN
http://search.cpan.org/dist/HASH-Sanitize/
Copyright 2012 Evozon Systems
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Hash::Sanitize, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Hash::Sanitize
CPAN shell
perl -MCPAN -e shell install Hash::Sanitize
For more information on module installation, please visit the detailed CPAN module installation guide.