| 1 |
NAME |
| 2 |
|
| 3 |
Hash::StoredIterator - Functions for accessing a hashes internal |
| 4 |
iterator. |
| 5 |
|
| 6 |
DESCRIPTION |
| 7 |
|
| 8 |
In perl all hashes have an internal iterator. This iterator is used by |
| 9 |
the each() function, as well as by keys() and values(). Because these |
| 10 |
all share use of the same iterator, they tend to interact badly with |
| 11 |
each other when nested. |
| 12 |
|
| 13 |
Hash::StoredIterator gives you access to get, set, and init the |
| 14 |
iterator inside a hash. This allows you to store the current iterator, |
| 15 |
use each/keys/values/etc, and then restore the iterator, this helps you |
| 16 |
to ensure you do not interact badly with other users of the iterator. |
| 17 |
|
| 18 |
Along with low-level get/set/init functions, there are also 2 |
| 19 |
variations of each() which let you act upon each key/value pair in a |
| 20 |
safer way than vanilla each() |
| 21 |
|
| 22 |
This module can also export new implementations of keys() and values() |
| 23 |
which stash and restore the iterator so that they are safe to use |
| 24 |
within each(). |
| 25 |
|
| 26 |
SYNOPSIS |
| 27 |
|
| 28 |
use Hash::StoredIterator qw{ |
| 29 |
hmap |
| 30 |
hkeys |
| 31 |
hvalues |
| 32 |
iterator |
| 33 |
hash_get_iterator |
| 34 |
hash_set_iterator |
| 35 |
hash_init_iterator |
| 36 |
}; |
| 37 |
|
| 38 |
my %hash = map { $_ => uc( $_ )} 'a' .. 'z'; |
| 39 |
|
| 40 |
my @keys = hkeys %hash; |
| 41 |
my @values = hvalues %hash; |
| 42 |
|
| 43 |
Each section below is functionally identical. |
| 44 |
|
| 45 |
my $iterator = iterator %hash; |
| 46 |
while( my ( $k, $v ) = $i->() ) { |
| 47 |
print "$k: $value\n"; |
| 48 |
} |
| 49 |
|
| 50 |
hmap { print "$a: $b\n" } %hash; |
| 51 |
|
| 52 |
hamp { print "$_: $b\n" } %hash; |
| 53 |
|
| 54 |
hmap { |
| 55 |
my ( $key, $val ) = @_; |
| 56 |
print "$key: $val\n"; |
| 57 |
} %hash; |
| 58 |
|
| 59 |
It is safe to nest calls to hmap(), iterator(), hkeys(), and hvalues() |
| 60 |
|
| 61 |
hmap { |
| 62 |
my ( $key, $val ) = @_; |
| 63 |
print "$key: $val\n"; |
| 64 |
my @keys = hkeys( %hash ); |
| 65 |
} %hash; |
| 66 |
|
| 67 |
hmap() and iterator() will also properly handle calls to CORE::each, |
| 68 |
CORE::keys, and Core::values nested within them. |
| 69 |
|
| 70 |
hmap { |
| 71 |
my ( $key, $val ) = @_; |
| 72 |
print "$key: $val\n"; |
| 73 |
|
| 74 |
# No infinite loop! |
| 75 |
my @keys = keys %hash; |
| 76 |
} %hash; |
| 77 |
|
| 78 |
Low Level: |
| 79 |
|
| 80 |
hash_init_iterator( \%hash ); |
| 81 |
my $iter = hash_get_iterator( \%hash ); |
| 82 |
# NOTE: Never manually specify an $iter value, ALWAYS use a value from |
| 83 |
# hash_get_iterator. |
| 84 |
hash_set_iterator( \%hash, $iter ); |
| 85 |
|
| 86 |
EXPORTS |
| 87 |
|
| 88 |
my $i = iterator %hash |
| 89 |
|
| 90 |
Get an iterator that can be used to retrieve key/value pairs. |
| 91 |
|
| 92 |
my $i = iterator %hash; |
| 93 |
while( my ($k, $v) = $i->() ) { |
| 94 |
... |
| 95 |
} |
| 96 |
|
| 97 |
The iterator is a coderef, so you call it like this: $i-()>. You can |
| 98 |
also use the sub anywhere you would use any other coderef. |
| 99 |
|
| 100 |
hmap( \&callback, %hash ) |
| 101 |
|
| 102 |
hmap { ... } %hash |
| 103 |
|
| 104 |
Iterate each key/pair calling $callback-( $key, $value )> for each |
| 105 |
set. In addition $a and $_ are set to the key, and $b is set to the |
| 106 |
value. This is done primarily for convenience of matching against the |
| 107 |
key, and short callbacks that will be cluttered by parsing @_ noise. |
| 108 |
|
| 109 |
Note: See caveats. |
| 110 |
|
| 111 |
my @keys = hkeys( %hash ) |
| 112 |
|
| 113 |
Same as the builtin keys(), except it stores and restores the |
| 114 |
iterator. |
| 115 |
|
| 116 |
Note: Overriding the builtin keys(), even locally, causes strange |
| 117 |
interactions with other builtins. When trying to export hkeys as |
| 118 |
keys, a call to sort keys %hash would cause undef to be passed into |
| 119 |
keys() as the first and only argument. |
| 120 |
|
| 121 |
my @values = hvalues( %hash ) |
| 122 |
|
| 123 |
Same as the builtin values(), except it stores and restores the |
| 124 |
iterator. |
| 125 |
|
| 126 |
Note: Overriding the builtin values(), even locally, causes strange |
| 127 |
interactions with other builtins. When trying to export hvalues as |
| 128 |
values, a call to sort values %hash would cause undef to be passed |
| 129 |
into values() as the first and only argument. |
| 130 |
|
| 131 |
my $i = hash_get_iterator( \%hash ) |
| 132 |
|
| 133 |
Get the current iterator value. |
| 134 |
|
| 135 |
hash_set_iterator( \%hash, $i ) |
| 136 |
|
| 137 |
Set the iterator value. |
| 138 |
|
| 139 |
Note: Only ever set this to the value retrieved by |
| 140 |
hash_get_iterator(), setting the iterator in any other way is |
| 141 |
untested, and may result in undefined behavior. |
| 142 |
|
| 143 |
hash_init_iterator( \%hash ) |
| 144 |
|
| 145 |
Initialize or reset the hash iterator. |
| 146 |
|
| 147 |
DEPRECATED |
| 148 |
|
| 149 |
These have been deprecated because they were terrible names. eich was |
| 150 |
also deprecated because it was unnatural to use. |
| 151 |
|
| 152 |
eich |
| 153 |
|
| 154 |
use iterator() instead |
| 155 |
|
| 156 |
eech |
| 157 |
|
| 158 |
use hmap instead |
| 159 |
|
| 160 |
CAVEATS |
| 161 |
|
| 162 |
Modification of hash during iteration |
| 163 |
|
| 164 |
Just like with the builtin each() modifying the hash between calls to |
| 165 |
each is not recommended and can result in undefined behavior. The |
| 166 |
builtin each() does allow for deleting the iterations key, however |
| 167 |
that is NOT supported by this library. |
| 168 |
|
| 169 |
sort() edge case |
| 170 |
|
| 171 |
For some reason [sort hkeys %hash] and [sort hkeys(%hash)] both |
| 172 |
result in a list that has all the keys and values (and strangely not |
| 173 |
in sorted order). However [sort(hkeys(%hash))] works fine. |
| 174 |
|
| 175 |
AUTHORS |
| 176 |
|
| 177 |
Chad Granum [email protected] |
| 178 |
|
| 179 |
COPYRIGHT |
| 180 |
|
| 181 |
Copyright (C) 2013 Chad Granum |
| 182 |
|
| 183 |
Hash-StoredIterator is free software; Standard perl licence. |
| 184 |
|
| 185 |
Hash-StoredIterator is distributed in the hope that it will be useful, |
| 186 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 187 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license |
| 188 |
for more details. |
| 189 |
|