develooper Front page | perl.perl5.porters | Postings from July 2012

[perl #114024] perl mistakenly warns when $] indexes a slice

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
July 31, 2012 23:05
Subject:
[perl #114024] perl mistakenly warns when $] indexes a slice
Message ID:
rt-3.6.HEAD-11172-1343801106-778.114024-15-0@perl.org
On Tue Jul 31 08:55:25 2012, sprout wrote:
> On Fri Jul 06 18:01:00 2012, lponeil@math.umd.edu wrote:
> > This is a bug report for perl from lponeil@math.umd.edu,
> > generated with the help of perlbug 1.39 running under perl 5.14.2.
> > 
> > 
> > -----------------------------------------------------------------
> > [Please describe your issue here]
> > 
> > Hey Perl folks.
> > 
> > The following code dies, but shouldn't.
> > 
> >     use warnings FATAL => 'all';
> >     my @a = 0..9; print @a[$],0];
> > 
> > Larry.
> 
> Interesting:
> 
> $ ./perl -Ilib -Mwarnings=syntax -e '@a{$],0}'
> Scalar value @a{$] better written as $a{$] at -e line 1.
> 
> And:
> 
> $ ./perl -Ilib -Mwarnings=syntax -e '@a["]",0]'
> Scalar value @a["] better written as $a["] at -e line 1.
> $ ./perl -Ilib -Mwarnings=syntax -e '@a["}",0]'
> Scalar value @a["} better written as $a["} at -e line 1.
> 
> The code that produces this warning (case '@' in toke.c:yylex) scans the
> source following the [ or { for zero or more of these:
> 
>     alphanumerics space tab $ # + - ' "
> 
> followed by ] or }.
> 
> So it seems to be designed to catch:
> 
>  $a+1
>  -1
>  2
>  $#foo
>  --$_
>  $_++
>  'foo'
>  "foo"
> 
> But it also catches:
> 
>  foo  # could be a function returning a list
>  $]
>  "]"
>  "}"
>  qw"foo bar"  # see ticket #28380
> 
> but not ${]} (there is your workaround).
> 
> And warns for syntax errors (the syntax error is fine; the warning is
> weird):
> 
> $ ./perl -Ilib -Mwarnings=syntax -e '@a[]'
> Scalar value @a[] better written as $a[] at -e line 1.
> syntax error at -e line 1, near "[]"
> Execution of -e aborted due to compilation errors.
> 
> We could change it to skip the warning for the empty string or an
> initial alphanumeric character.  Instead of warning immediately, the
> lexer could flag the op, and the compiler could then look to see whether
> there is indeed just one kidop and warn.
> 
> I think that would work, without making the lexer’s heuristics overly
> complex.
> 
> Is there anything obviously wrong with that?

Yes: the source code cannot easily be accessed once one has an op.

Would it be acceptable to change the warning to use an ellipsis? 
(Scalar value @a[...] better written as $a[...].)

Would it be acceptable to remove this warning altogether?  The only case
that could cause a problem is @a[+foo], because foo could be called
unexpectedly in list context.  But that is precisely the case in which
perl cannot know that foo won’t return a list on purpose.  @a[0] is
generally harmless.  Is this supposed to be newbie patrol (to stop them
from using @a[0] all over the place)?

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=114024

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About