Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.93k stars 551 forks source link

Lexical scoping bug concerning eval #6309

Closed p5pRT closed 21 years ago

p5pRT commented 21 years ago

Migrated from rt.perl.org#21053 (status was 'resolved')

Searchable as RT21053$

p5pRT commented 21 years ago

From marcello@hummer.stanford.edu

Hi\, I am reporting a lexical scoping bug of a nasty nature. I am running Perl v5.6.1 on a redhat 7.3 i386-Linux platform. Synopsis​: Given a variable in package B\, used from package A\, package A can't access it in via an eval statement.

Example​: Here is the short and sweet (or not so sweet)! version​: __FILE__BEGINS__ #!/usr/bin/perl package B; my $answer = 42; 1; package A; sub fn{   print "answer=" . eval ('$B​::answer') . "\n";   print "answer=" . eval ('package B; $answer') . "\n"; } 1; package main; A​::fn(); 1; __FILE__ENDS__ this prints​: answer= answer=42

There is something way wrong as only the second one works! However\, there is more. In additional testing with A and B in separate files\, neither of them work! A tgz of the three files (A.pm\, B.pm\, and main.pl) is attached.

I would very much appreciate to know the cause of this aberration\, and have a personal interest in knowing how and why this code fails\, given that I want to use the same trick on something a bit more complicated and was quite chagrined that it didn't work.

I hope that this bug report will be of benefit to the Perl community. -=+Marcello Mathias Herreshoff

P.S. output of perlbug -d Site configuration information for perl v5.6.1​:

Configured by bhcompile at Mon Apr 1 12​:22​:19 EST 2002.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration​:   Platform​:   osname=linux\, osvers=2.4.17-0.13smp\, archname=i386-linux   uname='linux daffy.perf.redhat.com 2.4.17-0.13smp #1 smp fri feb 1 10​:30​:48 est 2002 i686 unknown '   config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc -Dcf_by=Red Hat\, Inc. -Dcccdlflags=-fPIC -Dinstallprefix=/usr -Dprefix=/usr -Darchna me=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Uusethreads -Uuseithreads -Uuselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_s hadow -Di_syslog -Dman3ext=3pm'   hint=recommended\, useposix=true\, d_sigaction=define   usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef   useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef   use64bitint=undef use64bitall=undef uselongdouble=undef   Compiler​:   cc='gcc'\, ccflags ='-fno-strict-aliasing -I/usr/local/include'\,   optimize='-O2 -march=i386 -mcpu=i686'\,   cppflags='-fno-strict-aliasing -I/usr/local/include'   ccversion=''\, gccversion='2.96 20000731 (Red Hat Linux 7.2 2.96-109)'\, 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=4   alignbytes=4\, usemymalloc=n\, prototype=define   Linker and Libraries​:   ld='gcc'\, ldflags =' -L/usr/local/lib'   libpth=/usr/local/lib /lib /usr/lib   libs=-lnsl -ldl -lm -lc -lcrypt -lutil   perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil   libc=/lib/libc-2.2.5.so\, so=so\, useshrplib=false\, libperl=libperl.a   Dynamic Linking​:   dlsrc=dl_dlopen.xs\, dlext=so\, d_dlsymun=undef\, ccdlflags='-rdynamic'   cccdlflags='-fPIC'\, lddlflags='-shared -L/usr/local/lib'

Locally applied patches​:


@​INC for perl v5.6.1​:   /usr/lib/perl5/5.6.1/i386-linux   /usr/lib/perl5/5.6.1   /usr/lib/perl5/site_perl/5.6.1/i386-linux   /usr/lib/perl5/site_perl/5.6.1   /usr/lib/perl5/site_perl/5.6.0/i386-linux   /usr/lib/perl5/site_perl/5.6.0   /usr/lib/perl5/site_perl   /usr/lib/perl5/vendor_perl/5.6.1/i386-linux   /usr/lib/perl5/vendor_perl/5.6.1   /usr/lib/perl5/vendor_perl   .


Environment for perl v5.6.1​:   HOME=/home/marcello   LANG=en_US   LANGUAGE (unset)   LD_LIBRARY_PATH (unset)   LOGDIR (unset)   PATH=/usr/kerberos/bin​:/usr/local/bin​:/bin​:/usr/bin​:/usr/X11R6/bin​:/home/marcello/prog/perl/​:/home/marcello/bin​:/home/marcello/prog/perl/​:/home/marce llo/bin   PERL_BADLANG (unset)   SHELL=/bin/bash

p5pRT commented 21 years ago

From marcello@hummer.stanford.edu

bug.tgz

p5pRT commented 21 years ago

From @iabyn

On Sat\, Feb 15\, 2003 at 03​:13​:43AM -0000\, marcello@​hummer.stanford.edu (via RT) wrote​:

Hi\, I am reporting a lexical scoping bug of a nasty nature. I am running Perl v5.6.1 on a redhat 7.3 i386-Linux platform. Synopsis​: Given a variable in package B\, used from package A\, package A can't access it in via an eval statement.

Example​: Here is the short and sweet (or not so sweet)! version​: __FILE__BEGINS__ #!/usr/bin/perl package B; my $answer = 42; 1; package A; sub fn{ print "answer=" . eval ('$B​::answer') . "\n"; print "answer=" . eval ('package B; $answer') . "\n"; } 1; package main; A​::fn(); 1; __FILE__ENDS__ this prints​: answer= answer=42

Thanks for the report; however\, this is not a bug\, as you have confused lexical variables with package (global) variables.

lexical variables are declared with 'my'\, and are valid only for the current lexical scope\, ie until the next block end\, sub end or file end\, whichever comes first - and they are not visible from other files. They do not live in any package.

Global variables are accessible from all files and all parts of a program; and are normally specified as package_name​::variable_name; however\, as a convenience\, you can drop the package_name​:: prefix is that happens to be the current package.

-- To collect all the latest movies\, simply place an unprotected ftp server on the Internet\, and wait for the disk to fill....

p5pRT commented 21 years ago

@iabyn - Status changed from 'new' to 'resolved'