Skip to content
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

foreach loop and local-to-file my variable #9785

Open
p5pRT opened this issue Jul 2, 2009 · 3 comments
Open

foreach loop and local-to-file my variable #9785

p5pRT opened this issue Jul 2, 2009 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 2, 2009

Migrated from rt.perl.org#67162 (status was 'open')

Searchable as RT67162$

@p5pRT
Copy link
Author

p5pRT commented Jul 2, 2009

From vincent-perl@vinc17.net

Created by vincent@vinc17.org

Note​: the following can be seen either as a perl bug or as
a documentation bug (but perl 5.001 had the same behavior
as perl 5.10.0).

Consider the following cases​:

$ perl -e 'foreach $i (1,2) { &foo } sub foo { print "!$i\n" }'
!1
!2
$ perl -e 'my $i; foreach $i (1,2) { &foo } sub foo { print "!$i\n" }'
!
!
$ perl -e 'my $i; foreach (1,2) { $i = $_; &foo } sub foo { print "!$i\n" }'
!1
!2

The result of the second test looks incorrect.

The perlsyn(1) man page says under "Foreach Loops"​:

  The "foreach" loop iterates over a normal list value and sets the
  variable VAR to be each element of the list in turn. If the variable
  is preceded with the keyword "my", then it is lexically scoped, and is
  therefore visible only within the loop. Otherwise, the variable is
  implicitly local to the loop and regains its former value upon exiting
  the loop. If the variable was previously declared with "my", it uses
  that variable instead of the global one, but it's still localized to
  the loop. This implicit localisation occurs only in a "foreach" loop.

For the second test, we are in the 3rd case​:

  If the variable was previously declared with "my", it uses that
  variable instead of the global one, but it's still localized to
  the loop.

The variable is *not* lexically scoped, so that the $i of the
  * my $i
  * foreach variable
  * in sub foo
lexically represent the same variable.

Now concerning its value, the variable is localized, but this is just
like in the first test​: the call to foo is done from the loop, and the
value should be the one assigned in the loop (dynamic scoping).

Perl Info

Flags:
    category=core
    severity=low

Site configuration information for perl 5.10.0:

Configured by Debian Project at Fri Jun 12 21:43:19 UTC 2009.

Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.6.26-1-vserver-amd64, archname=x86_64-linux-gnu-thread-multi
    uname='linux excelsior 2.6.26-1-vserver-amd64 #1 smp sat jan 10 19:46:42 utc 2009 x86_64 gnulinux '
    config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=x86_64-linux-gnu -Dprefix=/usr -Dprivlib=/usr/share/perl/5.10 -Darchlib=/usr/lib/perl/5.10 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.10.0 -Dsitearch=/usr/local/lib/perl/5.10.0 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Ud_ualarm -Uusesfio -Uusenm -DDEBUGGING=-g -Doptimize=-O2 -Duseshrplib -Dlibperl=libperl.so.5.10.0 -Dd_dosuid -des'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2 -g',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include'
    ccversion='', gccversion='4.3.3', 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 =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /lib64 /usr/lib64
    libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
    perllibs=-ldl -lm -lpthread -lc -lcrypt
    libc=/lib/libc-2.9.so, so=so, useshrplib=true, libperl=libperl.so.5.10.0
    gnulibc_version='2.9'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib'

Locally applied patches:
    


@INC for perl 5.10.0:
    /home/vlefevre/lib/site_perl/x86_64-linux-gnu-thread-multi
    /home/vlefevre/lib/site_perl
    /etc/perl
    /usr/local/lib/perl/5.10.0
    /usr/local/share/perl/5.10.0
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.10
    /usr/share/perl/5.10
    /usr/local/lib/site_perl
    .


Environment for perl 5.10.0:
    HOME=/home/vlefevre
    LANG=POSIX
    LANGUAGE (unset)
    LC_CHARMAP=ISO-8859-1
    LC_COLLATE=POSIX
    LC_CTYPE=en_US.ISO8859-1
    LC_TIME=en_DK
    LD_LIBRARY_PATH=/opt/sge/lib/UNSUPPORTED-lx24-GLIBC-2.9-amd64:/lib64:/home/vlefevre/x86_64/lib:/home/vlefevre/lib
    LOGDIR (unset)
    PATH=/home/vlefevre/bin:/home/vlefevre/x86_64/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/bin:/usr/games:.
    PERL5LIB=/home/vlefevre/lib/site_perl
    PERL_BADLANG (unset)
    SHELL=zsh

@p5pRT
Copy link
Author

p5pRT commented Jul 3, 2009

From @davidnicol

Inline Patch
diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod
index 2ba30d8..e466ea8 100644
--- a/pod/perlsyn.pod
+++ b/pod/perlsyn.pod
@@ -405,14 +405,12 @@ X<readline> X<< <> >>
 X<for> X<foreach>

 The C<foreach> loop iterates over a normal list value and sets the
-variable VAR to be each element of the list in turn.  If the variable
-is preceded with the keyword C<my>, then it is lexically scoped, and
-is therefore visible only within the loop.  Otherwise, the variable is
-implicitly local to the loop and regains its former value upon exiting
-the loop.  If the variable was previously declared with C<my>, it uses
-that variable instead of the global one, but it's still localized to
-the loop.  This implicit localisation occurs I<only> in a C<foreach>
-loop.
+variable VAR to be each element of the list in turn.  If the variable is
+preceded by or was previously declared with the keyword C<my>, then it
+is lexically scoped, and is visible only within the loop.  Otherwise,
+the variable is implicitly local to the loop and regains its former
+value upon exiting the loop.  This implicit localisation occurs I<only>
+in a C<foreach> loop.
 X<my> X<local>

 The C<foreach> keyword is actually a synonym for the C<for> keyword, so
@@ -424,7 +422,7 @@ X<$_>
 If any element of LIST is an lvalue, you can modify it by modifying
 VAR inside the loop.  Conversely, if any element of LIST is NOT an
 lvalue, any attempt to modify that element will fail.  In other words,
-the C<foreach> loop index variable is an implicit alias for each item
+the C<foreach> loop index variable gets aliased to each item
 in the list that you're looping over.
 X<alias>

@p5pRT
Copy link
Author

p5pRT commented Jul 3, 2009

The RT System itself - Status changed from 'new' to 'open'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants