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

Owner: Nobody
Requestors: eda [at] waniasset.com
Cc:
AdminCc:

Operating System: Linux
PatchStatus: (no value)
Severity: low
Type: core
Perl Version: 5.22.2
Fixed In: (no value)



To: <perlbug [...] perl.org>
Date: Fri, 24 Nov 2017 11:27:32 +0000
From: <eda [...] waniasset.com>
Subject: Missing warning 'scalar value better written as...' when using reference
Download (untitled) / with headers
text/plain 7.2k
This is a bug report for perl from eda@waniasset.com, generated with the help of perlbug 1.40 running under perl 5.22.2. ----------------------------------------------------------------- [Please describe your issue here] % perl -wE '@x = (1, 2, 3); say @x[1]' Scalar value @x[1] better written as $x[1] at -e line 1. 2 % perl -wE '$x = [ 1, 2, 3 ]; say @$x[1]' 2 The second use should also make a warning, along the lines of Scalar value @$x[1] better written as $x->[1] [Please do not change anything below this line] ----------------------------------------------------------------- --- Flags: category=core severity=low --- Site configuration information for perl 5.22.2: Configured by Red Hat, Inc. at Fri Nov 4 14:35:02 UTC 2016. Summary of my perl5 (revision 5 version 22 subversion 2) configuration: Platform: osname=linux, osvers=4.7.9-200.fc24.x86_64, archname=x86_64-linux-thread-multi uname='linux buildvm-12.phx2.fedoraproject.org 4.7.9-200.fc24.x86_64 #1 smp thu oct 20 14:26:16 utc 2016 x86_64 x86_64 x86_64 gnulinux ' config_args='-des -Doptimize=none -Dccflags=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Dldflags=-Wl,-z,relro -Dccdlflags=-Wl,--enable-new-dtags -Wl,-z,relro -Dlddlflags=-shared -Wl,-z,relro -Dshrpdir=/usr/lib64 -DDEBUGGING=-g -Dversion=5.22.2 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl5 -Dsitearch=/usr/local/lib64/perl5 -Dprivlib=/usr/share/perl5 -Dvendorlib=/usr/share/perl5/vendor_perl -Darchlib=/usr/lib64/perl5 -Dvendorarch=/usr/lib64/perl5/vendor_perl -Darchname=x86_64-linux-thread-multi -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64 -Duseshrplib -Dusethreads -Duseithreads -Dusedtrace=/usr/bin/dtrace -Duselargefiles -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl=n -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr -Dd_gethostent_r_proto -Ud_endhostent_r_proto -Ud_sethostent_r_proto -Ud_endprotoent_r_proto -Ud_setprotoent_r_proto -Ud_endservent_r_proto -Ud_setservent_r_proto -Dscriptdir=/usr/bin -Dusesitecustomize' hint=recommended, useposix=true, d_sigaction=define useithreads=define, usemultiplicity=define use64bitint=define, use64bitall=define, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fwrapv -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize=' -g', cppflags='-D_REENTRANT -D_GNU_SOURCE -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fwrapv -fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='5.3.1 20160406 (Red Hat 5.3.1-6)', gccosandvers='' intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='gcc', ldflags ='-Wl,-z,relro -fstack-protector-strong -L/usr/local/lib' libpth=/usr/local/lib64 /lib64 /usr/lib64 /usr/local/lib /usr/lib /lib/../lib64 /usr/lib/../lib64 /lib libs=-lpthread -lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat perllibs=-lpthread -lresolv -lnsl -ldl -lm -lcrypt -lutil -lc libc=libc-2.22.so, so=so, useshrplib=true, libperl=libperl.so gnulibc_version='2.22' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,--enable-new-dtags -Wl,-z,relro ' cccdlflags='-fPIC', lddlflags='-shared -Wl,-z,relro -L/usr/local/lib -fstack-protector-strong' Locally applied patches: Fedora Patch1: Removes date check, Fedora/RHEL specific Fedora Patch3: support for libdir64 Fedora Patch4: use libresolv instead of libbind Fedora Patch5: USE_MM_LD_RUN_PATH Fedora Patch6: Skip hostname tests, due to builders not being network capable Fedora Patch7: Dont run one io test due to random builder failures Fedora Patch15: Define SONAME for libperl.so Fedora Patch16: Install libperl.so to -Dshrpdir value Fedora Patch22: Document Math::BigInt::CalcEmu requires Math::BigInt (CPAN RT#85015) Fedora Patch26: Make *DBM_File desctructors thread-safe (RT#61912) Fedora Patch27: Make PadlistNAMES() lvalue again (CPAN RT#101063) Fedora Patch28: Make magic vtable writable as a work-around for Coro (CPAN RT#101063) Fedora Patch29: Fix duplicating PerlIO::encoding when spawning threads (RT#31923) Fedora Patch30: Do not let XSLoader load relative paths (CVE-2016-6185) Fedora Patch31: Avoid loading optional modules from default . (CVE-2016-1238) Fedora Patch32: Fix a crash in lexical scope warnings (RT#128597) Fedora Patch33: Do not mangle errno from failed socket calls (RT#128316) Fedora Patch34: Fix crash in "evalbytes S" (RT#129196) Fedora Patch35: Fix crash in "evalbytes S" (RT#129196) Fedora Patch36: Fix crash in "evalbytes S" (RT#129196) Fedora Patch37: Fix crash in splice (RT#129164, RT#129166, RT#129167) Fedora Patch38: Fix string overrun in Perl_gv_fetchmethod_pvn_flags (RT#129267) Fedora Patch39: Fix string overrun in Perl_gv_fetchmethod_pvn_flags (RT#129267) Fedora Patch40: Fix string overrun in Perl_gv_fetchmethod_pvn_flags (RT#129267) Fedora Patch41: Fix string overrun in Perl_gv_fetchmethod_pvn_flags (RT#129267) Fedora Patch42: Fix string overrun in Perl_gv_fetchmethod_pvn_flags (RT#129267) Fedora Patch43: Fix crash when matching UTF-8 string with non-UTF-8 substrings (RT#129350) Fedora Patch44: Fix parsing perl options in shell bang line (RT#129336) Fedora Patch45: Fix firstchar bitmap under UTF-8 with prefix optimization (RT#129950) Fedora Patch46: Avoid infinite loop in h2xs tool if enum and type have the same name (RT130001) Fedora Patch47: Fix stack handling when calling chdir without an argument (RT#129130) Fedora Patch200: Link XS modules to libperl.so with EU::CBuilder on Linux Fedora Patch201: Link XS modules to libperl.so with EU::MM on Linux --- @INC for perl 5.22.2: /home/eda/lib64/perl5/ /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 --- Environment for perl 5.22.2: HOME=/home/eda LANG=en_GB.UTF-8 LANGUAGE (unset) LC_COLLATE=C LC_CTYPE=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 LC_MONETARY=en_GB.UTF-8 LC_NUMERIC=en_GB.UTF-8 LC_TIME=en_GB.UTF-8 LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/home/eda/bin:/home/eda/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/sbin:/home/eda/.local/bin:/home/eda/bin:/sbin:/usr/sbin PERL5LIB=/home/eda/lib64/perl5/ PERL_BADLANG (unset) SHELL=/bin/bash
Date: Mon, 27 Nov 2017 10:18:16 +0100
To: "Ed Avis (via RT)" <perlbug-followup [...] perl.org>
Subject: Re: [perl #132502] Missing warning 'scalar value better written as...' when using reference
From: Sawyer X <xsawyerx [...] gmail.com>
Download (untitled) / with headers
text/plain 1.1k
On 11/24/2017 12:27 PM, Ed Avis (via RT) wrote: Show quoted text
> # New Ticket Created by "Ed Avis" > # Please include the string: [perl #132502] > # in the subject line of all future correspondence about this issue. > # <URL: https://rt.perl.org/Ticket/Display.html?id=132502 > > > > > This is a bug report for perl from eda@waniasset.com, > generated with the help of perlbug 1.40 running under perl 5.22.2. > > > ----------------------------------------------------------------- > [Please describe your issue here] > > % perl -wE '@x = (1, 2, 3); say @x[1]' > Scalar value @x[1] better written as $x[1] at -e line 1. > 2 > % perl -wE '$x = [ 1, 2, 3 ]; say @$x[1]' > 2 > > The second use should also make a warning, along the lines of > > Scalar value @$x[1] better written as $x->[1]
I think that's a preference in style. While I personally dislike the style, it is not a mistake to do so. The warning for "scalar value better written as" refers to the context and has an effect on assignment, which is a behavioral difference. @foo[ bar() ] = baz(); # List context $foo[ bar() ] = baz(); # scalar context So it's better to warn here because it's likely not what you wanted if you're explicitly accessing a single index.
From: Smylers <smylers [...] stripey.com>
Subject: Re: [perl #132502] Missing warning 'scalar value better written as...' when using reference
Date: Mon, 27 Nov 2017 09:50:01 +0000
To: "perlbug-followup" <perlbug-followup [...] perl.org>
Sawyer X <xsawyerx@gmail.com> writes: Show quoted text
> On 11/24/2017 12:27 PM, Ed Avis (via RT) wrote: >
> > % perl -wE '@x = (1, 2, 3); say @x[1]' > > Scalar value @x[1] better written as $x[1] at -e line 1. > > 2 > > % perl -wE '$x = [ 1, 2, 3 ]; say @$x[1]' > > 2 > > > > The second use should also make a warning, along the lines of > > > > Scalar value @$x[1] better written as $x->[1]
> > I think that's a preference in style. While I personally dislike the > style, it is not a mistake to do so. > > The warning for "scalar value better written as" refers to the context > and has an effect on assignment, which is a behavioral difference. > > @foo[ bar() ] = baz(); # List context > $foo[ bar() ] = baz(); # scalar context
That context difference still applies with references: @$foo[0] = qw<first middle last>; # 'first' $foo->[0] = qw<first middle last>; # 'last' The matter of style is that the scalar context one can also be written: $$foo[0] = qw<first middle last>; # 'last' Smylers
Download (untitled) / with headers
text/plain 212b
Sawyer X, thanks for your reply. If I understand you correctly, you say that the warning is intended for the use of @x[1] as an lvalue. What I do not understand is why it makes sense to treat @$x[1] differently.
From: Eirik Berg Hanssen <Eirik-Berg.Hanssen [...] allverden.no>
Subject: Re: [perl #132502] Missing warning 'scalar value better written as...' when using reference
Date: Mon, 27 Nov 2017 14:31:31 +0100
To: Smylers <smylers [...] stripey.com>
CC: perlbug-followup <perlbug-followup [...] perl.org>
Download (untitled) / with headers
text/plain 1.6k


On Mon, Nov 27, 2017 at 10:50 AM, Smylers <smylers@stripey.com> wrote:
Show quoted text
Sawyer X <xsawyerx@gmail.com> writes:

> On 11/24/2017 12:27 PM, Ed Avis (via RT) wrote:
>
> > % perl -wE '@x = (1, 2, 3); say @x[1]'
> > Scalar value @x[1] better written as $x[1] at -e line 1.
> > 2
> > % perl -wE '$x = [ 1, 2, 3 ]; say @$x[1]'
> > 2
> >
> > The second use should also make a warning, along the lines of
> >
> >   Scalar value @$x[1] better written as $x->[1]
>
> I think that's a preference in style. While I personally dislike the
> style, it is not a mistake to do so.
>
> The warning for "scalar value better written as" refers to the context
> and has an effect on assignment, which is a behavioral difference.
>
> @foo[ bar() ] = baz(); # List context
> $foo[ bar() ] = baz(); # scalar context

That context difference still applies with references:

  @$foo[0]  = qw<first middle last>;  # 'first'
  $foo->[0] = qw<first middle last>;  # 'last'

The matter of style is that the scalar context one can also be written:

  $$foo[0]  = qw<first middle last>;   # 'last'

  So, a less confusing warning would be: "Scalar value @$x[1] better written as $$x[1]".  (A minimal difference is more to the point, right?)

  Or for a more thorough approach: "Scalar value @$x[1] better written as $$x[1] (or equivalently, $x->[1])".


Eirik

Virus-free. www.avg.com
From: Sawyer X <xsawyerx [...] gmail.com>
Subject: Re: [perl #132502] Missing warning 'scalar value better written as...' when using reference
Date: Mon, 27 Nov 2017 21:28:37 +0100
CC: perlbug-followup <perlbug-followup [...] perl.org>
Download (untitled) / with headers
text/plain 1.6k
On 11/27/2017 02:31 PM, Eirik Berg Hanssen wrote: Show quoted text
> > > On Mon, Nov 27, 2017 at 10:50 AM, Smylers <smylers@stripey.com > <mailto:smylers@stripey.com>> wrote: > > Sawyer X <xsawyerx@gmail.com <mailto:xsawyerx@gmail.com>> writes: >
> > On 11/24/2017 12:27 PM, Ed Avis (via RT) wrote: > >
> > > % perl -wE '@x = (1, 2, 3); say @x[1]' > > > Scalar value @x[1] better written as $x[1] at -e line 1. > > > 2 > > > % perl -wE '$x = [ 1, 2, 3 ]; say @$x[1]' > > > 2 > > > > > > The second use should also make a warning, along the lines of > > > > > >   Scalar value @$x[1] better written as $x->[1]
> > > > I think that's a preference in style. While I personally dislike the > > style, it is not a mistake to do so. > > > > The warning for "scalar value better written as" refers to the
> context
> > and has an effect on assignment, which is a behavioral difference. > > > > @foo[ bar() ] = baz(); # List context > > $foo[ bar() ] = baz(); # scalar context
> > That context difference still applies with references: > >   @$foo[0]  = qw<first middle last>;  # 'first' >   $foo->[0] = qw<first middle last>;  # 'last' > > The matter of style is that the scalar context one can also be > written: > >   $$foo[0]  = qw<first middle last>;   # 'last' > > >   So, a less confusing warning would be: "Scalar value @$x[1] better > written as $$x[1]".  (A minimal difference is more to the point, right?) > >   Or for a more thorough approach: "Scalar value @$x[1] better written > as $$x[1] (or equivalently, $x->[1])".
Make it ${$x}[1] and you have got yourself a deal. :)


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