http://www.perlmonks.org?node_id=11112500


in reply to Iterating over an hash while removing keys

You don't have to call scalar. Using a void context resets the iterator without even checking the keys at all.
- scalar(keys %$hash); # Force reset + keys %$hash; # Force reset

Update: Documented in keys:

> In particular, calling "keys" in void context resets the iterator with no other overhead.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Iterating over an hash while removing keys
by Eily (Monsignor) on Feb 06, 2020 at 16:57 UTC

    That was actually my first version, and I forgot to ask about the behaviour of keys in void context (it was really likely that it didn't expand the list of keys needlessly, but better safe than sorry :) ).

    Thanks!