Hash::...erator(3pm)
πΈ Hash::StoredIterator β a.k.a. Hash::...erator(3pm) π
okay bestie, sit down, hydrate, because we're about to talk about the β¨internal iteratorβ¨ that lives inside every single Perl hash and is lowkey the reason your nested loops are a mess. This is the manual page for Hash::StoredIterator, and she is iconic.
π¦ NAME
Hash::StoredIterator β functions for accessing a hashes internal iterator. no cap, that's the whole job. π«Ά
β¨ DESCRIPTION
okay so. In perl all hashes have an internal iterator. Every single one. And this iterator is the thing that each() uses, and also keys() and values(). The way they all share the same iterator is exactly why they interact so badly with each other when you nest them. it's giving toxic polycule. π
Hash::StoredIterator gives you access to get, set, and init the iterator inside a hash. Which means you can store the current iterator, go do your each/keys/values/etc business, and then restore the iterator. That's how you make sure you're not ruining things for literally every other user of the iterator. mutual respect, periodt. π
Along with the low-level get/set/init functions, there are also 2 variations of each() which let you act upon each key/value pair in a safer way than vanilla each(). vanilla is fine but we're not doing vanilla here. π
This module can also export new implementations of keys() and values() which stash and restore the iterator so that they are safe to use within each(). slay.
π SYNOPSIS
use Hash::StoredIterator qw{
hmap
hkeys
hvalues
iterator
hash_get_iterator
hash_set_iterator
hash_init_iterator
};
my %hash = map { $_ => uc( $_ )} 'a' .. 'z';
my @keys = hkeys %hash;
my @values = hvalues %hash;
Each section below is functionally identical. fr fr, same energy, different outfit. π
my $iterator = iterator %hash;
while( my ( $k, $v ) = $i->() ) {
print "$k: $value\n";
}
hmap { print "$a: $b\n" } %hash;
hamp { print "$_: $b\n" } %hash;
hmap {
my ( $key, $val ) = @_;
print "$key: $val\n";
} %hash;
It is safe to nest calls to hmap(), iterator(), hkeys(), and hvalues(). nest away, bestie, go feral. πͺ
hmap {
my ( $key, $val ) = @_;
print "$key: $val\n";
my @keys = hkeys( %hash );
} %hash;
hmap() and iterator() will also properly handle calls to "CORE::each", "CORE::keys", and "Core::values" nested within them. yes the capitalisation there is exactly how the original says it, don't come for me. π
hmap {
my ( $key, $val ) = @_;
print "$key: $val\n";
# No infinite loop!
my @keys = keys %hash;
} %hash;
Low Level: the no-frills aisle. π
hash_init_iterator( \%hash );
my $iter = hash_get_iterator( \%hash );
# NOTE: Never manually specify an $iter value, ALWAYS use a value from
# hash_get_iterator.
hash_set_iterator( \%hash, $iter );
π EXPORTS
my $i = iterator %hashβ Get an iterator that can be used to retrieve key/value pairs. π¦
my $i = iterator %hash;
while( my ($k, $v) = $i->() ) {
...
}
The iterator is a coderef, so you call it like this: "$i-"()>. You can also use the sub anywhere you would use any other coderef. she's versatile like that. π
hmap( \&callback, %hash )/hmap { ... } %hashβ Iterate each key/pair calling"$callback-"( $key, $value )>for each set. In addition$aand$_are set to the key, and$bis set to the value. This is done primarily for convenience of matching against the key, and short callbacks that will be cluttered by parsing@_noise.
Note: See caveats. β οΈ (yes, those caveats. we'll get there.)
my @keys = hkeys( %hash )β Same as the builtinkeys(), except it stores and restores the iterator. β¨
Note: Overriding the builtin keys(), even locally, causes strange interactions with other builtins. When trying to export hkeys as keys, a call to "sort keys %hash" would cause undef to be passed into keys() as the first and only argument. π yeah. that.
my @values = hvalues( %hash )β Same as the builtinvalues(), except it stores and restores the iterator. πΈ
Note: Overriding the builtin values(), even locally, causes strange interactions with other builtins. When trying to export hvalues as values, a call to "sort values %hash" would cause undef to be passed into values() as the first and only argument. same curse, different function. π
my $i = hash_get_iterator( \%hash )β Get the current iterator value. π«Άhash_set_iterator( \%hash, $i )β Set the iterator value.
Note: Only ever set this to the value retrieved by hash_get_iterator(), setting the iterator in any other way is untested, and may result in undefined behavior. do NOT freestyle this one, bestie. I am begging. π
hash_init_iterator( \%hash )β Initialize or reset the hash iterator. clean slate, fresh start, main character reboot. π§
π DEPRECATED
These have been deprecated because they were terrible names. eich was also deprecated because it was unnatural to use. honestly? fair. the vibes were off. π«
eichβ useiterator()insteadeechβ usehmapinstead
β οΈ CAVEATS
Modification of hash during iteration
Just like with the builtin each(), modifying the hash between calls to each is not recommended and can result in undefined behavior. The builtin each() does allow for deleting the iterations key, however that is NOT supported by this library. so like. don't. π€
sort() edge case
For some reason "[sort hkeys %hash]" and "[sort hkeys(%hash)]" both result in a list that has all the keys and values (and strangely not in sorted order). However "[sort(hkeys(%hash))]" works fine. the parentheses are doing the lord's work here. π
π AUTHORS
Chad Granum <exodist7@gmail.com>
π COPYRIGHT
Copyright (C) 2013 Chad Granum
Hash-StoredIterator is free software; Standard perl licence.
Hash-StoredIterator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
and that's the whole page, bestie π this was perl v5.42.3, dated 2026-08-20, and the manual is Hash::...erator(3pm). go forth and nest your iterators responsibly. mwah. π«Άβ¨
π¦ where this page comes from
- ubuntu stonking: libhash-storediterator-perl 0.008-3build6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms