my $last = each %hash; while (defined $last) { my $next = each %hash; if (defined $next) { # $last is now set, but is _not_ the last, do what you want with it } else { # $last is now _the_ last, do what you want with it. } $last = $next; }