summaryrefslogtreecommitdiff
path: root/t/op
diff options
context:
space:
mode:
authorHojung Yoon <amoc.yn@gmail.com>2011-05-24 18:18:14 -0700
committerFather Chrysostomos <sprout@cpan.org>2011-05-24 20:08:32 -0700
commit459b64da6c5743fa9093a9ef5214a82b46f2f7d0 (patch)
treecd1efb6cc33c5a8f9dee02b6434b87103345c9f6 /t/op
parenteee8d40ab2eee78fd8a8a007a74b8b97b9f3c053 (diff)
downloadperl-459b64da6c5743fa9093a9ef5214a82b46f2f7d0.tar.gz
[perl #90888] each(ARRAY) on scalar context should wrapped into defined()
"perldoc -f each" says that if each() is performed on ARRAY in scalar context, it will return only the index in an array. Calling each(HASH) in scalar context worked well but calling each(ARRAY) didn't because it was not wrapped into defined OPCODE. So, in Perl_newWHILEOP() and Perl_newLOOPOP(), they are modified to check them and wrap with defined OP if needed. In S_new_logop(), it's reasonable to warn if return value of each(ARRAY) is being used for boolean value, as it's first return value will be "0", the false. issue: #90888 link: http://rt.perl.org/rt3/Public/Bug/Display.html?id=90888
Diffstat (limited to 't/op')
-rw-r--r--t/op/each_array.t45
1 files changed, 43 insertions, 2 deletions
diff --git a/t/op/each_array.t b/t/op/each_array.t
index 2389473725..9a6073ab75 100644
--- a/t/op/each_array.t
+++ b/t/op/each_array.t
@@ -8,9 +8,9 @@ BEGIN {
use strict;
use warnings;
no warnings 'deprecated';
-use vars qw(@array @r $k $v);
+use vars qw(@array @r $k $v $c);
-plan tests => 48;
+plan tests => 66;
@array = qw(crunch zam bloop);
@@ -132,3 +132,44 @@ is ("@values", "@array");
($k, $v) = each @array;
is ($k, 0);
is ($v, 'crunch');
+
+# reset
+$[ = 0;
+while (each @array) { }
+
+# each(ARRAY) in the conditional loop
+$c = 0;
+while (($k, $v) = each @array) {
+ is ($k, $c);
+ is ($v, $array[$k]);
+ $c++;
+}
+
+# each(ARRAY) on scalar context in conditional loop
+# should guarantee to be wrapped into defined() function.
+# first return value will be $[ --> [#90888]
+$c = 0;
+$k = 0;
+$v = 0;
+while ($k = each @array) {
+ is ($k, $v);
+ $v++;
+}
+
+# each(ARRAY) in the conditional loop
+$c = 0;
+for (; ($k, $v) = each @array ;) {
+ is ($k, $c);
+ is ($v, $array[$k]);
+ $c++;
+}
+
+# each(ARRAY) on scalar context in conditional loop
+# --> [#90888]
+$c = 0;
+$k = 0;
+$v = 0;
+for (; $k = each(@array) ;) {
+ is ($k, $v);
+ $v++;
+}