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 08:55
Subject:
[perl #114024] perl mistakenly warns when $] indexes a slice
Message ID:
rt-3.6.HEAD-11172-1343750125-1148.114024-15-0@perl.org
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?

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: new
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