Skip to content

Commit

Permalink
add a note to perldoc -f each on using anon hashes/arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
tonycoz committed Jul 6, 2021
1 parent e1d3ed9 commit 55da32a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pod/perlfunc.pod
Expand Up @@ -2012,6 +2012,15 @@ the iterator state used by anything else that might execute during the
loop body. To avoid these problems, use a C<foreach> loop rather than
C<while>-C<each>.

This extends to using C<each> on the result of an anonymous hash or
array constructor. A new underlying array or hash is created each
time so each will always start iterating from scratch, eg:

# loops forever
while (my ($key, $value) = each @{ +{ a => 1 } }) {
print "$key=$value\n";
}

This prints out your environment like the L<printenv(1)> program,
but in a different order:

Expand Down

0 comments on commit 55da32a

Please sign in to comment.