File Coverage

blib/lib/Acme/CPANModules/HashUtilities.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Acme::CPANModules::HashUtilities;
2              
3 1     1   334434 use strict;
  1         2  
  1         34  
4 1     1   412 use Acme::CPANModulesUtil::Misc;
  1         559  
  1         110  
5              
6             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7             our $DATE = '2023-10-29'; # DATE
8             our $DIST = 'Acme-CPANModules-HashUtilities'; # DIST
9             our $VERSION = '0.004'; # VERSION
10              
11             our $LIST = {
12             summary => "List of modules that manipulate hashes",
13             description => <<'_',
14              
15             Most of the time, you don't need modules to manipulate hashes; Perl's built-in
16             facilities suffice. The modules below, however, are sometimes convenient. This
17             list is organized by task.
18              
19             ## Creating an alias to another variable
20              
21             's C allows you to store an alias to a variable in a
22             hash instead of copying the value. This means, if you set a hash value, it will
23             instead set the value of the aliased variable instead. Copying from Hash::Util's
24             documentation:
25              
26             my $sv = 0;
27             hv_store(%hash,$key,$sv) or die "Failed to alias!";
28             $hash{$key} = 1;
29             print $sv; # prints 1
30              
31              
32             ## Getting internal information
33              
34             Aside from creating restricted hash, also provides routines to
35             get information about hash internals, e.g. `hash_seed()`, `hash_value()`,
36             `bucket_info()`, `bucket_stats()`, etc.
37              
38              
39             ## Merging
40              
41             Merging hashes is usually as simple as:
42              
43             my %merged = (%hash1, %hash2, %hash3);
44              
45             but sometimes you want different merging behavior, particularly in case where
46             the same key is found in more than one hash. See the various hash merging
47             modules:
48              
49            
50              
51            
52              
53            
54              
55              
56             ## Providing default value for non-existing keys
57              
58            
59              
60              
61             ## Restricting keys
62              
63             Perl through (a core module) allows you to restrict what keys
64             can be set in a hash. This can be used to protect against typos and for simple
65             validation. (For more complex validation, e.g. allowing patterns of valid keys
66             and/or rejecting patterns of invalid keys, you can use the tie mechanism.)
67              
68              
69             ## Reversing (inverting)
70              
71             Reversing a hash (where keys become values and values become keys) can be done
72             using the builtin's `reverse` (which actually just reverse a list):
73              
74             %hash = (a=>1, b=>2);
75             %reverse = reverse %hash; # => (2=>"b", 1=>"a")
76              
77             Since the new keys can contain duplicates, this can "destroy" some old keys:
78              
79             %hash = (a=>1, b=>1);
80             %reverse = reverse %hash; # => sometimes (1=>"b"), sometimes (1=>"a")
81              
82             's `safe_reverse` allows you to specify a coderef that can
83             decide whether to ignore overwriting, croak, or whatever else.
84              
85              
86             ## Slicing (creating subset)
87              
88             's `slice_*` functions.
89              
90            
91              
92            
93              
94              
95             ## Tying
96              
97             The tie mechanism, although relatively slow, allows you to create various kinds
98             of "magical" hash that does things whenever you get or set keys.
99              
100              
101             _
102             'x.app.cpanmodules.show_entries' => 0,
103             };
104              
105             Acme::CPANModulesUtil::Misc::populate_entries_from_module_links_in_description;
106              
107             1;
108             # ABSTRACT: List of modules that manipulate hashes
109              
110             __END__