Perl / perl5

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

${::foo} isn't handled correctly #7520

Closed p5pRT closed 19 years ago

p5pRT commented 19 years ago

Migrated from rt.perl.org#31797 (status was 'rejected')

Searchable as RT31797$

p5pRT commented 19 years ago

From matthew.harris@clearsightsystems.com

This is a bug report for perl from mharris@​litech.org\, generated with the help of perlbug 1.35 running under perl v5.8.5.


I can enclose a variable in braces to separate it from surrounding text​:

  $ perl -we '$foo="hi"; print "${foo}\n"'   hi

And I can use $​::foo to refer to a name in the 'main' package​:

  $ perl -we '$​::foo="hi"; print "$​::foo\n"'   hi

But if I try to do both together\, it produces the correct value but warns me as if I didn't reference the variable at all​:

  $ perl -we '$​::foo="hi"; print "${​::foo}\n"'   Name "main​::foo" used only once​: possible typo at -e line 1.   hi   $ perl -we '$​::foo="hi"; print "${main​::foo}\n"'   hi

(The fact that I'm interpolating isn't important; dropping the double quotes and the \n from the print statements gives the same results without the newlines.)

FWIW\, I don't know what $​::{foo} is supposed to do\, but I wouldn't have expected this result​:

  $ perl -we '$​::foo="hi"; print "$​::{foo}\n"'   Name "main​::foo" used only once​: possible typo at -e line 1.   *main​::foo



Flags​:   category=core   severity=low


Site configuration information for perl v5.8.5​:

Configured by Gentoo at Tue Sep 14 21​:33​:28 Local time zone must be set--see zic manual page 2004.

Summary of my perl5 (revision 5 version 8 subversion 5) configuration​:   Platform​:   osname=linux\, osvers=2.6.7-co-0.6.2\, archname=i586-linux   uname='linux colinux 2.6.7-co-0.6.2 #392 sat jul 10 19​:59​:22 idt 2004 i686 intel(r) pentium(r) 4 cpu 2.40ghz genuineintel gnulinux '   config_args='-des -Darchname=i586-linux -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth= -Doptimize=-march=i586 -funroll-loops -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/var/tmp/portage/perl-5.8.5/image//usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dcf_by=Gentoo -Ud_csh -Ui_ndbm -Ui_gdbm -Ui_db'   hint=recommended\, useposix=true\, d_sigaction=define   usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef   useperlio=define d_sfio=undef uselargefiles=define usesocks=undef   use64bitint=undef use64bitall=undef uselongdouble=undef   usemymalloc=n\, bincompat5005=undef   Compiler​:   cc='gcc'\, ccflags ='-fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'\,   optimize='-march=i586 -funroll-loops'\,   cppflags='-DPERL5 -fno-strict-aliasing -pipe'   ccversion=''\, gccversion='3.3.4 20040623 (Gentoo Linux 3.3.4-r1\, ssp-3.3.2-2\, pie-8.7.6)'\, 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='gcc'\, ldflags =' -L/usr/local/lib'   libpth=/usr/local/lib /lib /usr/lib   libs=-lpthread -lnsl -lndbm -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc   perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc   libc=/lib/libc-2.3.4.so\, so=so\, useshrplib=false\, libperl=libperl.a   gnulibc_version='2.3.4'   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.8.5​:   /etc/perl   /usr/lib/perl5/site_perl/5.8.5/i586-linux   /usr/lib/perl5/site_perl/5.8.5   /usr/lib/perl5/site_perl/5.8.4/i586-linux   /usr/lib/perl5/site_perl/5.8.4   /usr/lib/perl5/site_perl/5.8.3/i586-linux   /usr/lib/perl5/site_perl/5.8.3   /usr/lib/perl5/site_perl/5.8.2/i586-linux   /usr/lib/perl5/site_perl/5.8.2   /usr/lib/perl5/site_perl/5.8.0   /usr/lib/perl5/site_perl   /usr/lib/perl5/vendor_perl/5.8.5/i586-linux   /usr/lib/perl5/vendor_perl/5.8.5   /usr/lib/perl5/vendor_perl/5.8.3/i586-linux   /usr/lib/perl5/vendor_perl/5.8.3   /usr/lib/perl5/vendor_perl/5.8.2/i586-linux   /usr/lib/perl5/vendor_perl/5.8.2   /usr/lib/perl5/vendor_perl   /usr/lib/perl5/5.8.5/i586-linux   /usr/lib/perl5/5.8.5   /usr/local/lib/site_perl   /usr/lib/perl5/site_perl/5.8.4/i586-linux   /usr/lib/perl5/site_perl/5.8.4   /usr/lib/perl5/site_perl/5.8.3/i586-linux   /usr/lib/perl5/site_perl/5.8.3   /usr/lib/perl5/site_perl/5.8.2/i586-linux   /usr/lib/perl5/site_perl/5.8.2   /usr/lib/perl5/site_perl/5.8.0   .


Environment for perl v5.8.5​:   HOME=/home/mharris   LANG (unset)   LANGUAGE (unset)   LD_LIBRARY_PATH (unset)   LOGDIR (unset)

PATH=/bin​:/usr/bin​:/usr/local/bin​:/opt/bin​:/usr/i586-pc-linux-gnu/gcc-bi n/3.4​:/usr/X11R6/bin​:/opt/blackdown-jdk-1.4.2_rc1/bin​:/opt/blackdown-jdk -1.4.2_rc1/jre/bin   PERL_BADLANG (unset)   SHELL=/bin/bash

p5pRT commented 19 years ago

From @rgs

Matthew Harris wrote​:

I can enclose a variable in braces to separate it from surrounding text​:

$ perl -we '$foo="hi"; print "${foo}\n"' hi

And I can use $​::foo to refer to a name in the 'main' package​:

$ perl -we '$​::foo="hi"; print "$​::foo\n"' hi

But if I try to do both together\, it produces the correct value but warns me as if I didn't reference the variable at all​:

$ perl -we '$​::foo="hi"; print "${​::foo}\n"' Name "main​::foo" used only once​: possible typo at -e line 1. hi

Here\, the second statement tries to look up a variable whose name is "​::foo"\, not "foo".

FWIW\, I don't know what $​::{foo} is supposed to do\, but I wouldn't have expected this result​:

It's simply a hash lookup in %​:: (a.k.a. the symbol table\, documented in the perlmod manpage.)

p5pRT commented 19 years ago

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

p5pRT commented 19 years ago

@smpeters - Status changed from 'open' to 'rejected'

p5pRT commented 19 years ago

From matthew.harris@clearsightsystems.com

Thanks for your quick response.

Rafael Garcia-Suarez wrote​:

Matthew Harris wrote​:

$ perl -we '$​::foo="hi"; print "${​::foo}\n"' Name "main​::foo" used only once​: possible typo at -e line 1. hi

Here\, the second statement tries to look up a variable whose name is "​::foo"\, not "foo".

Then why does it still print "hi"? Is it normal for Perl to try to look up more than one variant of a variable name?

Even if this is what it's doing\, is this correct behavior? My reasoning is this​: ${main​::foo} evaluates to $main​::foo\, and according to the first paragraph of perlmod the "main" can always be omitted\, so ${​::foo} should be identical. Can you point me to a source (e.g.\, somewhere in the Perl documentation\, or an instance of DWIM) that explains the observed behavior?

p5pRT commented 19 years ago

From nick@ing-simmons.net

Matthew Harris \matthew\.harris@​clearsightsystems\.com writes​:

Thanks for your quick response.

Rafael Garcia-Suarez wrote​:

Matthew Harris wrote​:

$ perl -we '$​::foo="hi"; print "${​::foo}\n"' Name "main​::foo" used only once​: possible typo at -e line 1. hi

Here\, the second statement tries to look up a variable whose name is "​::foo"\, not "foo".

Then why does it still print "hi"? Is it normal for Perl to try to look up more than one variant of a variable name?

nick@​llama​:/home/p4work/TI/RTLGen> perl -we 'use strict; $​::foo="hi"; print "${​::foo}\n"' Bareword "​::foo" not allowed while "strict subs" in use at -e line 1.

${​::foo} is a symbolic reference and resolves to $main​::foo at run time. The "used only once" is reported at end of compile phase when it _has_ only been used once.

Even if this is what it's doing\, is this correct behavior? My reasoning is this​: ${main​::foo} evaluates to $main​::foo\, and according to the first paragraph of perlmod the "main" can always be omitted\, so ${​::foo} should be identical. Can you point me to a source (e.g.\, somewhere in the Perl documentation\, or an instance of DWIM) that explains the observed behavior?

p5pRT commented 19 years ago

From @davidnicol

nick@​llama​:/home/p4work/TI/RTLGen> perl -we 'use strict; $​::foo="hi"; print "${​::foo}\n"' Bareword "​::foo" not allowed while "strict subs" in use at -e line 1.

${​::foo} is a symbolic reference and resolves to $main​::foo at run time. The "used only once" is reported at end of compile phase when it _has_ only been used once.

:​:foo is a static expression known at compile time\, why isn't it optimized?

-- David L Nicol "Valuable ideas can withstand scrutiny" -- authors of /XP Explained/

p5pRT commented 19 years ago

From nick@ing-simmons.net

David Nicol \davidnicol@​gmail\.com writes​:

nick@​llama​:/home/p4work/TI/RTLGen> perl -we 'use strict; $​::foo="hi"; print "${​::foo}\n"' Bareword "​::foo" not allowed while "strict subs" in use at -e line 1.

${​::foo} is a symbolic reference and resolves to $main​::foo at run time. The "used only once" is reported at end of compile phase when it _has_ only been used once.

​::foo is a static expression known at compile time\, why isn't it optimized?

I was reporting what happens not why ;-)

Doesn't alter the fact that ${"main​::foo"} is a symbolic ref and needs to bind to any re-assigns to the glob that occur.