From 55da32af519c9e4420e964e07ed08e5d804bdbb9 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Wed, 17 Mar 2021 14:23:29 +1100 Subject: [PATCH] add a note to perldoc -f each on using anon hashes/arrays --- pod/perlfunc.pod | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 0b0953a44d09..502af8aba2bd 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -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 loop rather than C-C. +This extends to using C 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 program, but in a different order: