Perl / perl5

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

my() and my show different result in scalar context #1168

Closed p5pRT closed 20 years ago

p5pRT commented 24 years ago

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

Searchable as RT2136$

p5pRT commented 24 years ago

From inyeol@siimage.com

Created by inyeol@siimage.com

(my($foo) = $bar) =~ tr/ //d; # wrong answer

and

(my $foo = $bar) =~ tr/ //d; # correct answer

show different results. Since $bar is a scalar variable\, there's no ambiguity whether the context is a scalar or a list\, so the two results should be the same. Is it a bug?

Perl Info ``` Site configuration information for perl 5.00503: Configured by inyeol at Mon Aug 30 00:21:45 PDT 1999. Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration: Platform: osname=solaris, osvers=2.5.1, archname=sun4-solaris uname='sunos reno 5.5.1 generic sun4u sparc sunw,ultra-1 ' hint=recommended, useposix=true, d_sigaction=define usethreads=undef useperlio=undef d_sfio=undef Compiler: cc='gcc', optimize='-O', gccversion=2.8.1 cppflags='-I/usr/local/include' ccflags ='-I/usr/local/include' stdchar='unsigned char', d_stdstdio=define, usevfork=false intsize=4, longsize=4, ptrsize=4, doublesize=8 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16 alignbytes=8, usemymalloc=y, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib libs=-lsocket -lnsl -ldl -lm -lc -lcrypt libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' ' cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib' Locally applied patches: @INC for perl 5.00503: /home/inyeol/util/perl/lib/5.00503/sun4-solaris /home/inyeol/util/perl/lib/5.00503 /home/inyeol/util/perl/lib/site_perl/5.005/sun4-solaris /home/inyeol/util/perl/lib/site_perl/5.005 . Environment for perl 5.00503: HOME=/home/inyeol LANG=C LANGUAGE (unset) LC_ALL=C LD_LIBRARY_PATH=/usr/lib:/usr/ccs/lib:/usr/ucblib:/usr/4lib:/usr/openwin/lib:/usr/dt/lib:/home/inyeol/util/lib LOGDIR (unset) PATH=/home/inyeol/util/bin:/home/inyeol/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:/usr/ucb:/usr/openwin/bin:/usr/dt/bin:/usr/local/bin:/home/inyeol/tools/bin PERL_BADLANG (unset) SHELL=/home/inyeol/util/bin/tcsh ```
p5pRT commented 24 years ago

From [Unknown Contact. See original ticket]

On Feb 10\, Inyeol Lee said​:

generated with the help of perlbug 1.26 running under perl 5.00503.

(my($foo) = $bar) =~ tr/ //d; # wrong answer (my $foo = $bar) =~ tr/ //d; # correct answer

(these test are in Perl 5.005_02)

This is independent of my​:

  $b = "a b c";   (($a) = $b) =~ tr/ //d; print $a; # a b c   ($a = $b) =~ tr/ //d; print $a; # abc

  $a = "a b c";   [$a]->[0] =~ tr/ //d; print $a; # a b c

Notice this error message though​:

  $a = "a b c";   ($a)[0] =~ tr/ //d; print $a; # ERROR​:   # Can't modify list slice in character translation near "tr/ //d;"

  $a = "a b c";   @​{[ $a ]}[0] =~ tr/ //d; print $a; # a b c

Sigh... the left-hand side is being VERY picky.

-- MIDN 4/C PINYAN\, NROTCURPI\, US Naval Reserve japhy@​pobox.com http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/ PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/ The Perl Archive - Articles\, Forums\, etc. http​://www.perlarchive.com/

p5pRT commented 24 years ago

From @TimToady

japhy@​pobox.com writes​: : On Feb 10\, Inyeol Lee said​: : : >generated with the help of perlbug 1.26 running under perl 5.00503. : : >(my($foo) = $bar) =~ tr/ //d; # wrong answer : >(my $foo = $bar) =~ tr/ //d; # correct answer : : (these test are in Perl 5.005_02) : : This is independent of my​: : : $b = "a b c"; : (($a) = $b) =~ tr/ //d; print $a; # a b c

That's a list assignment in scalar context\, so it produces the count of arguments on its right side\, which is 1\, which you then apply the tr/// to. So what it does is expected (by me :-)\, whether or not the my is there.

: ($a = $b) =~ tr/ //d; print $a; # abc

Also expected.

: $a = "a b c"; : [$a]->[0] =~ tr/ //d; print $a; # a b c

The list composer [$a] makes a copy of $a\, which the tr/// successfully changes\, rather than $a.

: Notice this error message though​: : : $a = "a b c"; : ($a)[0] =~ tr/ //d; print $a; # ERROR​: : # Can't modify list slice in character translation near "tr/ //d;"

That could possibly be made to work\, but the demand for fancy lvalues is never very high.

: $a = "a b c"; : @​{[ $a ]}[0] =~ tr/ //d; print $a; # a b c

Again\, your list composer makes a copy of $a.

: Sigh... the left-hand side is being VERY picky.

Only with the list slice. The rest are working as designed.

Larry