[perl #90888] each(ARRAY) on scalar context should wrapped into defined()
authorHojung Yoon <amoc.yn@gmail.com>
Wed, 25 May 2011 01:18:14 +0000 (18:18 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 25 May 2011 03:08:32 +0000 (20:08 -0700)
"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
op.c
t/op/each_array.t

diff --git a/op.c b/op.c
index 0d4e1e6..e1bf353 100644 (file)
--- a/op.c
+++ b/op.c
@@ -5103,7 +5103,8 @@ S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
            if (k1->op_type == OP_READDIR
                  || k1->op_type == OP_GLOB
                  || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
-                 || k1->op_type == OP_EACH)
+                 || k1->op_type == OP_EACH
+                 || k1->op_type == OP_AEACH)
            {
                warnop = ((k1->op_type == OP_NULL)
                          ? (OPCODE)k1->op_targ : k1->op_type);
@@ -5347,7 +5348,8 @@ Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
                if (k1 && (k1->op_type == OP_READDIR
                      || k1->op_type == OP_GLOB
                      || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
-                     || k1->op_type == OP_EACH))
+                     || k1->op_type == OP_EACH
+                     || k1->op_type == OP_AEACH))
                    expr = newUNOP(OP_DEFINED, 0, expr);
                break;
            }
@@ -5435,7 +5437,8 @@ Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
                if (k1 && (k1->op_type == OP_READDIR
                      || k1->op_type == OP_GLOB
                      || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
-                     || k1->op_type == OP_EACH))
+                     || k1->op_type == OP_EACH
+                     || k1->op_type == OP_AEACH))
                    expr = newUNOP(OP_DEFINED, 0, expr);
                break;
            }
index 2389473..9a6073a 100644 (file)
@@ -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++;
+}