Acme-CPANModules-HashUtilities
view release on metacpan or search on metacpan
NAME
Acme::CPANModules::HashUtilities - List of modules that manipulate
hashes
VERSION
This document describes version 0.004 of
Acme::CPANModules::HashUtilities (from Perl distribution
Acme-CPANModules-HashUtilities), released on 2023-10-29.
DESCRIPTION
Most of the time, you don't need modules to manipulate hashes; Perl's
built-in facilities suffice. The modules below, however, are sometimes
convenient. This list is organized by task.
Creating an alias to another variable
Hash::Util's "hv_store" allows you to store an alias to a variable in a
hash instead of copying the value. This means, if you set a hash value,
it will instead set the value of the aliased variable instead. Copying
from Hash::Util's documentation:
my $sv = 0;
hv_store(%hash,$key,$sv) or die "Failed to alias!";
$hash{$key} = 1;
print $sv; # prints 1
Getting internal information
Aside from creating restricted hash, Hash::Util also provides routines
to get information about hash internals, e.g. hash_seed(), hash_value(),
bucket_info(), bucket_stats(), etc.
Merging
Merging hashes is usually as simple as:
my %merged = (%hash1, %hash2, %hash3);
but sometimes you want different merging behavior, particularly in case
where the same key is found in more than one hash. See the various hash
merging modules:
Hash::Merge
Data::ModeMerge
Hash::Union
Providing default value for non-existing keys
Hash::WithDefault
Restricting keys
Perl through Hash::Util (a core module) allows you to restrict what keys
can be set in a hash. This can be used to protect against typos and for
simple validation. (For more complex validation, e.g. allowing patterns
of valid keys and/or rejecting patterns of invalid keys, you can use the
tie mechanism.)
Reversing (inverting)
Reversing a hash (where keys become values and values become keys) can
be done using the builtin's "reverse" (which actually just reverse a
list):
%hash = (a=>1, b=>2);
%reverse = reverse %hash; # => (2=>"b", 1=>"a")
Since the new keys can contain duplicates, this can "destroy" some old
keys:
%hash = (a=>1, b=>1);
%reverse = reverse %hash; # => sometimes (1=>"b"), sometimes (1=>"a")
Hash::MoreUtil's "safe_reverse" allows you to specify a coderef that can
decide whether to ignore overwriting, croak, or whatever else.
Slicing (creating subset)
Hash::MoreUtils's "slice_*" functions.
Hash::Subset
Hash::Util::Pick
Tying
The tie mechanism, although relatively slow, allows you to create
various kinds of "magical" hash that does things whenever you get or set
keys.
ACME::CPANMODULES ENTRIES
Hash::Util
Author: RJBS <https://metacpan.org/author/RJBS>
Hash::Merge
Author: HERMES <https://metacpan.org/author/HERMES>
Data::ModeMerge
Author: PERLANCAR <https://metacpan.org/author/PERLANCAR>
Hash::Union
Author: LONERR <https://metacpan.org/author/LONERR>
Hash::WithDefault
Hash::MoreUtil
Hash::MoreUtils
Author: REHSACK <https://metacpan.org/author/REHSACK>
Hash::Subset
Author: PERLANCAR <https://metacpan.org/author/PERLANCAR>
Hash::Util::Pick
( run in 0.711 second using v1.01-cache-2.11-cpan-2398b32b56e )