in reply to Re^11: Why does each() always re-evaluate its argument? ("for_list" )
in thread Why does each() always re-evaluate its argument?
For-loop over an array will silently skip over deleted elements, hence a for-loop over a hash should too. (no tested yet)
If a warning should be activated, then in both cases alike.
unfortunately it's NOT consistent
$ cat tst_del.pl use v5.14; use experimental "for_list"; my %h; @h{"a".."d"} = 1..4; my @a = values %h; my $once; $once = 0; for my $e (@a) { splice @a,0,2 unless $once++; say "A: $e"; } $once = 0; for my ($k,$v) (%h) { delete @h{"a".."b"} unless $once++; say "H: $k => $v"; } say "never reached"; $ perl tst_del.pl A: 1 A: 3 H: a => 1 H: d => 4 Use of freed value in iteration at tst_del.pl line 22. $ perl -v This is perl 5, version 38, subversion 2 (v5.38.2)
tho, according to from perlsyn it's undefined behavior
If any part of LIST is an array, "foreach" will get very confused +if you add or remove elements within the loop body, for example with "spl +ice". So don't do that.
and the demonstrated results are quite unpredictable.
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
|
|---|