Hash::SafeKeys(3pm)
π Hash::SafeKeys π
okay bestie, welcome to Hash::SafeKeys, which lives in the User Contributed Perl Documentation zone (it's a 3pm page, iykyk π
). short version: it lets you peek at what's inside a hash without messing up the iterator you're currently walking. and like, if you've ever had a while ... each loop spin forever, this module is your emotional support bestie. π«Ά
πΈ VERSION πΈ
Version 0.04 β lil baby but mighty β¨
π SYNOPSIS π
use Hash::SafeKeys;
while (my ($k,$v) = each %hash) {
if (something_interesting_happens()) {
# get keys, values of %hash without resetting
# the 'each' iterator above
my @k = safekeys %hash;
my @v = safevalues %hash;
my %copy = safecopy %hash;
}
}
π¦ DESCRIPTION π¦
okay so here's the tea, and it's important tea β
every single hash variable in Perl has its own internal iterator, and it gets used by the builtins each, keys, and values. plot twist: that iterator is ALSO used implicitly any time the hash gets evaluated in list context. no escape. π
and whenever you call keys or values on a hash β including those sneaky implicit calls from list context β the iterator gets reset. which means doing certain hash operations inside a while ... each loop is genuinely dangerous territory:
while (my($k,$v) = each %hash) {
...
@k = sort keys %hash; # Infinite loop!
@v = grep { /foo/ }, values %hash; # Ack!
print join ' ', %hash; # Run away!
}
literally run away, bestie, don't look back π
Hash::SafeKeys said "not on my watch" and provides alternate functions to access the keys, the values, or the entire contents of a hash in a way that does not reset the iterator β so they're totally safe to use in those exact contexts:
while (my($k,$v) = each %hash) {
...
@k = sort safekeys %hash; # Can do
@v = grep { /foo/ }, safevalues %hash; # No problem
print join ' ', safecopy %hash; # Right away, sir
}
periodt. π
β¨ FUNCTIONS β¨
π safekeys
LIST = safekeys HASH
just like the builtin keys function β it hands you a list of all the keys of the named hash, in the same order the builtin would give them to you. unlike keys though, calling safekeys does not reset the HASH's internal iterator (see each). iconic behavior. π
π§ safevalues
LIST = safevalues HASH
like the builtin values function, it returns a list of all the values of the named hash, same order as the builtin would return them in. and unlike values, calling safevalues does not reset the HASH's internal iterator (see each). slay. β¨
π safecopy
LIST = safecopy HASH
in list context, this returns a shallow copy of the named HASH without resetting the HASH's internal iterator. usually, evaluating a HASH in list context implicitly uses the internal iterator, which would reset any existing state β and we simply do not do that here. π«
π save_iterator_state
π restore_iterator_state
HANDLE = save_iterator_state($hashref)
restore_iterator_state($hashref, HANDLE)
low-level functions for manipulating the iterator of a hash reference. the use cases for reaching for these directly are⦠let me count them on my sparkly nails:
- Performance ποΈ
the absolute fastest way to safely access the keys of a hash is:
$handle = Hash::Safekeys::save_iterator_state( \%hash );
@keys = keys %hash;
Hash::Safekeys::restore_iterator_state( \%hash, $handle );
this is an improvement over @keys = safekeys %hash because it eliminates the O(n) list copy operation on return from the safekeys function. no notes, it just eats. π₯
- Access to aliased values πͺ
the builtin values function returns aliases to the internal hash values, which means you can modify the contents of the hash with constructions like
s/foo/bar/g for values %hash
but as safevalues %hash returns a copy of the hash values, s/foo/bar/g for safevalues %hash will not modify the contents of the hash. sad but true π
to safely modify the values of the hash, a workaround with the low-level iterator functions is
$handle = Hash::SafeKeys::save_iterator_state( \%hash );
for (values %hash) { ... modify($_) ... }
Hash::SafeKeys::restore_iterator_state( \%hash, $handle );
- Nested each calls on the same hash πͺ
this construction will not work if $hash1 and $hash2 refer to the same hash:
while (($key1,$val1) = each %$hash1) {
while (($key2,$val2) = each %$hash2) { ... }
}
but this construction is safe:
while (($key1,$val1) = each %$hash1) {
$handle = Hash::SafeKeys::save_iterator_state($hash2);
while (($key2,$val2) = each %$hash2) { ... }
Hash::SafeKeys::restore_iterator_state($hash2, $handle);
}
the HANDLE that gets returned by save_iterator_state and used as an input to restore_iterator_state is currently implemented as an integer that can be mapped internally to an original hash iterator. this implementation is subject to change in future releases and you should not rely on this value being an integer. π
it is a grave error to provide a different hash reference with the handle to therestore_iterator_statecall than you provided to thesave_iterator_statecall that created the handle. do not do this. i am begging. π
callingsave_iterator_statewithout later callingrestore_iterator_statewill leak memory. that's not a vibe, that's a leak, bestie. π
π EXPORT π
safekeys, safevalues, and safecopy are all exported by default. if you don't want these functions imported into the calling package, invoke Hash::SafeKeys with the empty arg list:
use Hash::SafeKeys ();
the low-level iterator functions save_iterator_state and restore_iterator_state may also be exported β either by including them in the use call, or by using the tag :all:
use Hash::SafeKeys ':all'; # also exports low-level iterator funcs
choose your fighter π₯
π«Ά AUTHOR π«Ά
Marty O'Brien, "<mob at cpan.org>" β thank you for your service, king π
π BUGS π
please report any bugs or feature requests to "bug-hash-safekeys at rt.cpan.org", or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-SafeKeys. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. we love a feedback loop. β¨
πΈ SUPPORT πΈ
you can find documentation for this module with the perldoc command.
perldoc Hash::SafeKeys
you can also look for information at:
- RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-SafeKeys
- AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Hash-SafeKeys
- CPAN Ratings
http://cpanratings.perl.org/d/Hash-SafeKeys
- Search CPAN
http://search.cpan.org/dist/Hash-SafeKeys/
π§ ACKNOWLEDGEMENTS π§
the dclone method in the Storable module demonstrated how to save and restore internal hash iterator state. this module is indebted to the authors of that module, and to user gpojd at stackoverflow.com (http://stackoverflow.com/a/10921567/168857) for directing me to it. absolute legends, both. π
a helpful comment by Alexandr Evstigneev (http://search.cpan.org/~hurricup/) led to further improvements. we stay improving. π
π LICENSE AND COPYRIGHT π
Copyright 2012-2016 Marty O'Brien.
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.
and that's the whole page, bestie π this was perl v5.42.3, dated 2026-08-19, brought to you by the fine folks who write User Contributed Perl Documentation. mwah. πβ¨
π¦ where this page comes from
- ubuntu stonking: libhash-safekeys-perl 0.04-1build5 Β· 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