Skip Menu |
 
Report information
Id: 90888
Status: open
Priority: 0/
Queue: perl5

Owner: Nobody
Requestors: joel.a.berger [at] gmail.com
Cc:
AdminCc:

Operating System: Linux
PatchStatus: Applied
Severity: low
Type:
  • core
  • Patch
Perl Version: 5.14.0
Fixed In: (no value)

Attachments
0001-perl-90888-each-ARRAY-on-scalar-context-should-wrapp.patch



Subject: Using each @array as while iterator exits on first (0th) index
Date: Tue, 17 May 2011 12:12:49 -0500
To: perlbug [...] perl.org
From: Joel Berger <joel.a.berger [...] gmail.com>
Download (untitled) / with headers
text/plain 3.9k
This is a bug report for perl from joel.a.berger@gmail.com, generated with the help of perlbug 1.39 running under perl 5.14.0. ----------------------------------------------------------------- [Please describe your issue here] The new functionality which allows `each` to be called on an array lacks one useful design element. When called as `while (my $i = each @array) { ... }` the loop exits on the first iteration since the first index is 0. A brief discussion can be found at http://stackoverflow.com/q/6033750/468327 . In it another poster mentions that "`perl -MO=Deparse -E'...'` tells me that `while (my $i = each %h)` is deparsed as `while(defined(my $i = each %h))`, but the same isn't true for arrays." I think the principal of least confusion would state that using `while(each @array)` should not die for the 0th element. Thanks for this new feature, I have been eagerly awaiting being able to iterate over the indices of an array. One more change and it will be perfect! [Please do not change anything below this line] ----------------------------------------------------------------- --- Flags: category=core severity=low --- Site configuration information for perl 5.14.0: Configured by joel at Sun Apr 24 19:51:41 CDT 2011. Summary of my perl5 (revision 5 version 14 subversion 0) configuration: Platform: osname=linux, osvers=2.6.35-28-generic, archname=x86_64-linux uname='linux joel-ubuntu 2.6.35-28-generic #50-ubuntu smp fri mar 18 18:42:20 utc 2011 x86_64 gnulinux ' config_args='-de -Dprefix=/home/joel/perl5/perlbrew/perls/perl-5.14.0-RC1' hint=recommended, useposix=true, d_sigaction=define useithreads=undef, usemultiplicity=undef useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef use64bitint=define, use64bitall=define, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2', cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include' ccversion='', gccversion='4.4.5', gccosandvers='' intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='cc', ldflags =' -fstack-protector -L/usr/local/lib' libpth=/usr/local/lib /lib/../lib /usr/lib/../lib /lib /usr/lib /usr/lib/x86_64-linux-gnu /lib64 /usr/lib64 libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.12.1.so, so=so, useshrplib=false, libperl=libperl.a gnulibc_version='2.12.1' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E' cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector' Locally applied patches: RC1 --- @INC for perl 5.14.0: /home/joel/Programs/Perl5Lib /home/joel/perl5/perlbrew/perls/perl-5.14.0-RC1/lib/site_perl/5.14.0/x86_64-linux /home/joel/perl5/perlbrew/perls/perl-5.14.0-RC1/lib/site_perl/5.14.0 /home/joel/perl5/perlbrew/perls/perl-5.14.0-RC1/lib/5.14.0/x86_64-linux /home/joel/perl5/perlbrew/perls/perl-5.14.0-RC1/lib/5.14.0 . --- Environment for perl 5.14.0: HOME=/home/joel LANG=en_US.utf8 LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/home/joel/perl5/perlbrew/bin:/home/joel/perl5/perlbrew/perls/current/bin:/home/joel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/joel/Programs/Perl5Lib PERL5LIB=/home/joel/Programs/Perl5Lib PERLBREW_PATH=/home/joel/perl5/perlbrew/bin:/home/joel/perl5/perlbrew/perls/current/bin PERLBREW_PERL=perl-5.14.0-RC1 PERLBREW_ROOT=/home/joel/perl5/perlbrew PERLBREW_VERSION=0.18 PERL_BADLANG (unset) SHELL=/bin/bash
Subject: [perl #90888] each(ARRAY) on scalar context should wrapped into defined()
Date: Wed, 25 May 2011 05:15:16 +0900
To: perlbug [...] perl.org
From: am0c am0c <am0c [...] perl.kr>
Download (untitled) / with headers
text/plain 8.2k
commit 329521539b6f17f285367182890c6b24a7fdf408 Author: Hojung Yoon <amoc.yn@gmail.com> Date:   Wed May 25 03:41:53 2011 +0900    [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 commit ceb12f1f0217ba8594a71e20be18f84b4356adba Author: Johan Vromans <jvromans@squirrel.nl> Date:   Tue May 24 08:35:37 2011 -0700    [perl #91518] Fix minor typo in pod/perlsub.pod. ---------------------------------------- From 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 ---------------------------------------- --- Flags:    category=core    severity=medium --- 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:    osname=linux, osvers=2.6.32-31-generic, archname=i686-linux-thread-multi    uname='linux amolap 2.6.32-31-generic #61-ubuntu smp fri apr 8 18:24:35 utc 2011 i686 gnulinux '    config_args='-de -Dprefix=/home/am0c/perl5/perlbrew/perls/perl-5.14.0 -Dusethreads'    hint=recommended, useposix=true, d_sigaction=define    useithreads=define, usemultiplicity=define    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef    use64bitint=undef, use64bitall=undef, uselongdouble=undef    usemymalloc=n, bincompat5005=undef  Compiler:    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',    optimize='-O2',    cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'    ccversion='', gccversion='4.4.3', gccosandvers=''    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8    alignbytes=4, prototype=define  Linker and Libraries:    ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'    libpth=/usr/local/lib /lib/../lib /usr/lib/../lib /lib /usr/lib /usr/lib/i486-linux-gnu /usr/lib64    libs=-lnsl -ldb -ldl -lm -lcrypt -lutil -lpthread -lc    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc    libc=/lib/libc-2.11.1.so, so=so, useshrplib=false, libperl=libperl.a    gnulibc_version='2.11.1'  Dynamic Linking:    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'    cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector' Locally applied patches: --- @INC for perl 5.14.0:    /home/am0c/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0/i686-linux-thread-multi    /home/am0c/perl5/perlbrew/perls/perl-5.14.0/lib/site_perl/5.14.0    /home/am0c/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0/i686-linux-thread-multi    /home/am0c/perl5/perlbrew/perls/perl-5.14.0/lib/5.14.0    . --- Environment for perl 5.14.0:    HOME=/home/am0c    LANG=en_US.utf8    LANGUAGE (unset)    LD_LIBRARY_PATH (unset)    LOGDIR (unset)    PATH=/home/am0c/.rvm/gems/ruby-1.9.2-p0/bin:/home/am0c/.rvm/gems/ruby-1.9.2-p0@global/bin:/home/am0c/.rvm/rubies/ruby-1.9.2-p0/bin:/home/am0c/.rvm/bin:/home/am0c/perl5/perlbrew/bin:/home/am0c/perl5/perlbrew/perls/perl-5.14.0/bin:/home/am0c/local/android/android-sdk-linux_86/platform-tools:/home/am0c/bin:/home/am0c/local/android/android-sdk-linux_86/tools:/home/am0c/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games    PERLBREW_PATH=/home/am0c/perl5/perlbrew/bin:/home/am0c/perl5/perlbrew/perls/perl-5.14.0/bin    PERLBREW_PERL=perl-5.14.0    PERLBREW_ROOT=/home/am0c/perl5/perlbrew    PERLBREW_VERSION=0.21    PERL_BADLANG (unset)    SHELL=/bin/bash

Message body is not shown because sender requested not to inline it.

RT-Send-CC: perl5-porters [...] perl.org, amoc.yn [...] gmail.com
On Tue May 24 13:15:41 2011, am0c wrote: Show quoted text
> commit 329521539b6f17f285367182890c6b24a7fdf408 > Author: Hojung Yoon <amoc.yn@gmail.com> > Date: � Wed May 25 03:41:53 2011 +0900 > > � �[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
Thank you. Applied as 459b64d. I’m leaving this ticket open, until each $scalar is addressed.
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 1.2k
On Tue May 24 20:14:26 2011, sprout wrote: Show quoted text
> On Tue May 24 13:15:41 2011, am0c wrote:
> > commit 329521539b6f17f285367182890c6b24a7fdf408 > > Author: Hojung Yoon <amoc.yn@gmail.com> > > Date: � Wed May 25 03:41:53 2011 +0900 > > > > � �[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
> > Thank you. Applied as 459b64d. > > I’m leaving this ticket open, until each $scalar is addressed.
Does anyone know what it would take to handle the 'each $scalar' case? Thank you very much. Jim Keenan
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 1.4k
On Fri Feb 15 18:39:02 2013, jkeenan wrote: Show quoted text
> On Tue May 24 20:14:26 2011, sprout wrote:
> > On Tue May 24 13:15:41 2011, am0c wrote:
> > > commit 329521539b6f17f285367182890c6b24a7fdf408 > > > Author: Hojung Yoon <amoc.yn@gmail.com> > > > Date: � Wed May 25 03:41:53 2011 +0900 > > > > > > � �[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
> > > > Thank you. Applied as 459b64d. > > > > I’m leaving this ticket open, until each $scalar is addressed.
> > Does anyone know what it would take to handle the 'each $scalar' case? > > Thank you very much. > Jim Keenan
Probably checking for OP_REACH as well as OP_AEACH. -- Father Chrysostomos


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org