each
Returns the next key-value pair from a hash’s internal iterator.
Advances the hash’s internal iterator and returns the next entry.
In list context, pushes a (key, value) pair. In scalar context,
returns only the key. When the iterator is exhausted, returns an
empty list (list context) or undef (scalar context), and the
iterator resets automatically for the next traversal.
Note: keys and values reset the same internal iterator, so
calling them during an each loop will restart iteration.
Synopsis
while (my ($k, $v) = each %hash) { ... }
while (my $k = each %hash) { ... } # scalar context: key only
Implementation Notes
perl5 (pp.c): Returns (key, value) pair using the hash’s internal iterator. Returns empty list when iterator is exhausted. Does NOT reset the iterator â
keys %hashor iterator exhaustion resets it.