summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorTony Cook <tony@develop-help.com>2021-03-15 17:17:13 +1100
committerTony Cook <tony@develop-help.com>2021-07-06 10:47:15 +1000
commite1d3ed996ab025cea38d04e4751ee57ac200de85 (patch)
tree545a6d01c2351d773cda9536ac1b146abcaa6bdd /t
parentf7b332245abf1aa8ca9c82cce454353f7b820adb (diff)
downloadperl-e1d3ed996ab025cea38d04e4751ee57ac200de85.tar.gz
warn when using each on an anonymous hash or array
We've had three tickets over the years where the user has been confused by the behaviour of each on an anonymous array or hash, there's no way to tell if other users have been struck by the same issue, so make it easier to diagnose by producing a warning.
Diffstat (limited to 't')
-rw-r--r--t/lib/warnings/op80
1 files changed, 80 insertions, 0 deletions
diff --git a/t/lib/warnings/op b/t/lib/warnings/op
index 0012e74e52..20a5f0c2d2 100644
--- a/t/lib/warnings/op
+++ b/t/lib/warnings/op
@@ -2063,3 +2063,83 @@ Useless use of a constant (32) in void context at - line 11.
Useless use of a constant (41) in void context at - line 14.
Useless use of a constant (42) in void context at - line 14.
Useless use of a constant (51) in void context at - line 16.
+########
+# NAME warn on each on anonymous hash (simple)
+{
+ while (my ($k, $v) = each %{ +{ a => 1 }}) {
+ print $k, "\n";
+ last;
+ }
+}
+use warnings;
+{
+ while (my ($k, $v) = each %{ +{ b => 1 }}) {
+ print $k, "\n";
+ last;
+ }
+}
+EXPECT
+each on anonymous hash will always start from the beginning at - line 9.
+a
+b
+########
+# NAME warn each on anonymous hash (more complex)
+{
+ while (my ($k, $v) = each %{; print "c\n"; +{ a => 1 } }) {
+ print $k, "\n";
+ last;
+ }
+}
+use warnings;
+{
+ while (my ($k, $v) = each %{; print "d\n"; +{ b => 1 } }) {
+ print $k, "\n";
+ last
+ }
+}
+EXPECT
+each on anonymous hash will always start from the beginning at - line 9.
+c
+a
+d
+b
+########
+# NAME warn on each on anonymous array (simple)
+{
+ while (my ($k, $v) = each @{ [ "a", "b" ] }) {
+ print $v, "\n";
+ last;
+ }
+}
+use warnings;
+{
+ while (my ($k, $v) = each @{ [ "b", "a" ] }) {
+ print $v, "\n";
+ last;
+ }
+}
+EXPECT
+each on anonymous array will always start from the beginning at - line 9.
+a
+b
+########
+# NAME warn on each on anonymous array (more complex)
+{
+ while (my ($k, $v) = each @{; print "c\n"; [ "a", "b" ] }) {
+ print $v, "\n";
+ last;
+ }
+}
+use warnings;
+{
+ while (my ($k, $v) = each @{; print "d\n"; [ "b", "a" ] }) {
+ print $v, "\n";
+ last;
+ }
+}
+EXPECT
+each on anonymous array will always start from the beginning at - line 9.
+c
+a
+d
+b