-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using each @array as while iterator exits on first (0th) index #11361
Comments
From joel.a.berger@gmail.comCreated by joel.a.berger@gmail.comThe new functionality which allows `each` to be called on an array lacks one Thanks for this new feature, I have been eagerly awaiting being able Perl Info
|
From am0c@perl.krcommit 329521539b6f17f285367182890c6b24a7fdf408 [perl #90888] each(ARRAY) on scalar context should wrapped into defined() "perldoc -f each" says that if each() is performed on ARRAY So, in Perl_newWHILEOP() and Perl_newLOOPOP(), they are modified In S_new_logop(), it's reasonable to warn if return value of issue: #90888 commit ceb12f1 [perl #91518] Fix minor typo in pod/perlsub.pod. From 329521539b6f17f285367182890c6b24a7fdf408 Mon Sep 17 00:00:00 2001 "perldoc -f each" says that if each() is performed on ARRAY So, in Perl_newWHILEOP() and Perl_newLOOPOP(), they are modified In S_new_logop(), it's reasonable to warn if return value of issue: #90888 op.c | 9 ++++++--- Inline Patchdiff --git a/op.c b/op.c
index 0d4e1e6..8d333e9 100644
--- a/op.c
+++ b/op.c
@@ -5103,7 +5103,8 @@ S_new_logop(pTHX_ I32 type, I32 flags, OP**
Inline Patchdiff --git a/t/op/each_array.t b/t/op/each_array.t
index 2389473..9a6073a 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++;
+}
--
Flags: Site configuration information for perl 5.14.0: Configured by am0c at Mon May 23 09:46:57 KST 2011. Summary of my perl5 (revision 5 version 14 subversion 0) configuration: Platform: Locally applied patches: @INC for perl 5.14.0: Environment for perl 5.14.0: |
From am0c@perl.kr0001-perl-90888-each-ARRAY-on-scalar-context-should-wrapp.patchFrom 329521539b6f17f285367182890c6b24a7fdf408 Mon Sep 17 00:00:00 2001
From: Hojung Yoon <amoc.yn@gmail.com>
Date: Wed, 25 May 2011 03:41:53 +0900
Subject: [PATCH] [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
---
op.c | 9 ++++++---
t/op/each_array.t | 45 +++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 49 insertions(+), 5 deletions(-)
diff --git a/op.c b/op.c
index 0d4e1e6..8d333e9 100644
--- 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;
}
diff --git a/t/op/each_array.t b/t/op/each_array.t
index 2389473..9a6073a 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++;
+}
--
1.7.0.4
|
The RT System itself - Status changed from 'new' to 'open' |
From @cpansproutOn Tue May 24 13:15:41 2011, am0c wrote:
Thank you. Applied as 459b64d. I’m leaving this ticket open, until each $scalar is addressed. |
From @jkeenanOn Tue May 24 20:14:26 2011, sprout wrote:
Does anyone know what it would take to handle the 'each $scalar' case? Thank you very much. |
From @cpansproutOn Fri Feb 15 18:39:02 2013, jkeenan wrote:
Probably checking for OP_REACH as well as OP_AEACH. -- Father Chrysostomos |
From zefram@fysh.orgFather Chrysostomos wrote:
The experimental each($scalar) feature has been removed. This ticket -zefram |
@arc - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#90888 (status was 'resolved')
Searchable as RT90888$
The text was updated successfully, but these errors were encountered: