Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

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

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


perlbug-followup at perl

Jul 31, 2012, 8:55 AM

Post #1 of 10 (137 views)
Permalink
[perl #114024] perl mistakenly warns when $] indexes a slice

On Fri Jul 06 18:01:00 2012, lponeil [at] math wrote:
> This is a bug report for perl from lponeil [at] math,
> 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


lponeil at math

Jul 31, 2012, 10:18 AM

Post #2 of 10 (132 views)
Permalink
[perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

Hello Father Chrysostomos.

> making the lexer's heuristics overly complex.

My officemate just called this a "gross understatement", which i
thought you'd find amusing. :-). The bug report was academic and not
use-based, but i'll read your email in more detail and respond later.
Thanks.

Larry.


perlbug-followup at perl

Jul 31, 2012, 11:05 PM

Post #3 of 10 (127 views)
Permalink
[perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Tue Jul 31 08:55:25 2012, sprout wrote:
> On Fri Jul 06 18:01:00 2012, lponeil [at] math wrote:
> > This is a bug report for perl from lponeil [at] math,
> > 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


liz at dijkmat

Aug 1, 2012, 12:31 AM

Post #4 of 10 (129 views)
Permalink
Re: [perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Aug 1, 2012, at 8:05 AM, Father Chrysostomos via RT wrote:
> On Tue Jul 31 08:55:25 2012, sprout wrote:
>> $ ./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)?

I wonder whether this warning shouldn't be removed on the grounds that:

1. it is valid Perl 6 syntax, so what are we teaching newbies anyway?
2. there is only a performance difference between $a[0] and @a[0], or am I missing something there?



Liz


nick at ccl4

Aug 1, 2012, 10:59 AM

Post #5 of 10 (132 views)
Permalink
Re: [perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Wed, Aug 01, 2012 at 09:31:03AM +0200, Elizabeth Mattijsen wrote:

> I wonder whether this warning shouldn't be removed on the grounds that:
>
> 1. it is valid Perl 6 syntax, so what are we teaching newbies anyway?

But it means something subtly different - there it's a regular lookup, here
it's a trivial slice. Although I'm not sure how often the difference matters.

I don't think we should use "it's legal Perl 6 syntax" alone as grounds for
changing Perl 5 behaviour. For example, the analogous hash lookup syntax
%h{"k"} isn't legal Perl 5.

I think that the George Bernard Shaw quote "England and America are two
countries separated by a common language." applies to Perl 5 and Perl 6,
but I'm not quite sure how to paraphrase it. In that there are massive
amount of similarities between the two, but you need to treat them as
different, else you get into embarrassing mistakes. (eg "rubber")

> 2. there is only a performance difference between $a[0] and @a[0], or am I missing something there?

With a little bit of experimenting I did manage to find one difference:

$ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
$ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
Type of arg 1 to main::foo must be one of [$@] (not array slice) at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
$

Nicholas Clark


perlbug-followup at perl

Aug 1, 2012, 12:49 PM

Post #6 of 10 (132 views)
Permalink
[perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Wed Aug 01 10:59:45 2012, nicholas wrote:
> On Wed, Aug 01, 2012 at 09:31:03AM +0200, Elizabeth Mattijsen wrote:
>
> > I wonder whether this warning shouldn't be removed on the grounds
> that:
> >
> > 1. it is valid Perl 6 syntax, so what are we teaching newbies
> anyway?
>
> But it means something subtly different - there it's a regular lookup,
> here
> it's a trivial slice. Although I'm not sure how often the difference
> matters.
>
> I don't think we should use "it's legal Perl 6 syntax" alone as
> grounds for
> changing Perl 5 behaviour. For example, the analogous hash lookup
> syntax
> %h{"k"} isn't legal Perl 5.
>
> I think that the George Bernard Shaw quote "England and America are
> two
> countries separated by a common language." applies to Perl 5 and Perl
> 6,
> but I'm not quite sure how to paraphrase it. In that there are massive
> amount of similarities between the two, but you need to treat them as
> different, else you get into embarrassing mistakes. (eg "rubber")
>
> > 2. there is only a performance difference between $a[0] and @a[0],
> or am I missing something there?
>
> With a little bit of experimenting I did manage to find one
> difference:
>
> $ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
> $ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
> Type of arg 1 to main::foo must be one of [$@] (not array slice) at -e
> line 1, at EOF
> Execution of -e aborted due to compilation errors.
> $

The most significant different is, of course, assignment thereto. It
changes the context of the RHS. It is actually useful though, which is
probably why I usually find myself turning off syntax warnings:

@_[0] = /(.*)/

--

Father Chrysostomos


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


liz at dijkmat

Aug 1, 2012, 12:54 PM

Post #7 of 10 (128 views)
Permalink
Re: [perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Aug 1, 2012, at 7:59 PM, Nicholas Clark wrote:
> On Wed, Aug 01, 2012 at 09:31:03AM +0200, Elizabeth Mattijsen wrote:
>> I wonder whether this warning shouldn't be removed on the grounds that:
>> 1. it is valid Perl 6 syntax, so what are we teaching newbies anyway?
>
> But it means something subtly different - there it's a regular lookup, here
> it's a trivial slice. Although I'm not sure how often the difference matters.
>
> I don't think we should use "it's legal Perl 6 syntax" alone as grounds for
> changing Perl 5 behaviour. For example, the analogous hash lookup syntax
> %h{"k"} isn't legal Perl 5.

Then maybe it should be. :-)

Seriously, from Perl trainers I've always understood that it is always the most difficult thing for newbies to grasp. Which is why Larry decided to stick with the sigil for the type of data structure, regardless of the access / context.


> I think that the George Bernard Shaw quote "England and America are two
> countries separated by a common language." applies to Perl 5 and Perl 6,
> but I'm not quite sure how to paraphrase it. In that there are massive
> amount of similarities between the two, but you need to treat them as
> different, else you get into embarrassing mistakes. (eg "rubber")

And some of them you can't even express in writing. "You say potato, and I say potato". Gotcha.


>> 2. there is only a performance difference between $a[0] and @a[0], or am I missing something there?
> With a little bit of experimenting I did manage to find one difference:
>
> $ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
> $ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
> Type of arg 1 to main::foo must be one of [$@] (not array slice) at -e line 1, at EOF
> Execution of -e aborted due to compilation errors.

Wow, that's pretty obscure. I guess you *could* argue that that's a bug, but I won't :-)



Liz


liz at dijkmat

Aug 1, 2012, 12:57 PM

Post #8 of 10 (128 views)
Permalink
Re: [perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Aug 1, 2012, at 9:49 PM, Father Chrysostomos via RT wrote:
> On Wed Aug 01 10:59:45 2012, nicholas wrote:
>> On Wed, Aug 01, 2012 at 09:31:03AM +0200, Elizabeth Mattijsen wrote:
>>> I wonder whether this warning shouldn't be removed on the grounds
>> that:
>>>
>>> 1. it is valid Perl 6 syntax, so what are we teaching newbies
>> anyway?
>>
>> But it means something subtly different - there it's a regular lookup,
>> here
>> it's a trivial slice. Although I'm not sure how often the difference
>> matters.
>>
>> I don't think we should use "it's legal Perl 6 syntax" alone as
>> grounds for
>> changing Perl 5 behaviour. For example, the analogous hash lookup
>> syntax
>> %h{"k"} isn't legal Perl 5.
>>
>> I think that the George Bernard Shaw quote "England and America are
>> two
>> countries separated by a common language." applies to Perl 5 and Perl
>> 6,
>> but I'm not quite sure how to paraphrase it. In that there are massive
>> amount of similarities between the two, but you need to treat them as
>> different, else you get into embarrassing mistakes. (eg "rubber")
>>
>>> 2. there is only a performance difference between $a[0] and @a[0],
>> or am I missing something there?
>>
>> With a little bit of experimenting I did manage to find one
>> difference:
>>
>> $ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
>> $ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
>> Type of arg 1 to main::foo must be one of [$@] (not array slice) at -e
>> line 1, at EOF
>> Execution of -e aborted due to compilation errors.
>> $
>
> The most significant different is, of course, assignment thereto. It
> changes the context of the RHS. It is actually useful though, which is
> probably why I usually find myself turning off syntax warnings:
>
> @_[0] = /(.*)/

Aw, evil! But yes, an idiom that would probably be much more prevalent than the prototype issue.


Liz


perlbug-followup at perl

Aug 1, 2012, 8:21 PM

Post #9 of 10 (130 views)
Permalink
[perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Wed Aug 01 12:57:59 2012, elizabeth wrote:
> On Aug 1, 2012, at 9:49 PM, Father Chrysostomos via RT wrote:
> > On Wed Aug 01 10:59:45 2012, nicholas wrote:
> >> On Wed, Aug 01, 2012 at 09:31:03AM +0200, Elizabeth Mattijsen
> wrote:
> >>> I wonder whether this warning shouldn't be removed on the grounds
> >> that:
> >>>
> >>> 1. it is valid Perl 6 syntax, so what are we teaching newbies
> >> anyway?
> >>
> >> But it means something subtly different - there it's a regular
> lookup,
> >> here
> >> it's a trivial slice. Although I'm not sure how often the
> difference
> >> matters.
> >>
> >> I don't think we should use "it's legal Perl 6 syntax" alone as
> >> grounds for
> >> changing Perl 5 behaviour. For example, the analogous hash lookup
> >> syntax
> >> %h{"k"} isn't legal Perl 5.
> >>
> >> I think that the George Bernard Shaw quote "England and America are
> >> two
> >> countries separated by a common language." applies to Perl 5 and
> Perl
> >> 6,
> >> but I'm not quite sure how to paraphrase it. In that there are
> massive
> >> amount of similarities between the two, but you need to treat them
> as
> >> different, else you get into embarrassing mistakes. (eg "rubber")
> >>
> >>> 2. there is only a performance difference between $a[0] and @a[0],
> >> or am I missing something there?
> >>
> >> With a little bit of experimenting I did manage to find one
> >> difference:
> >>
> >> $ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
> >> $ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
> >> Type of arg 1 to main::foo must be one of [$@] (not array slice) at
> -e
> >> line 1, at EOF
> >> Execution of -e aborted due to compilation errors.
> >> $
> >
> > The most significant different is, of course, assignment thereto.
> It
> > changes the context of the RHS. It is actually useful though, which
> is
> > probably why I usually find myself turning off syntax warnings:
> >
> > @_[0] = /(.*)/
>
> Aw, evil! But yes, an idiom that would probably be much more
> prevalent than the prototype issue.

... were it not for the silly warning!


--

Father Chrysostomos


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


davem at iabyn

Aug 2, 2012, 5:58 AM

Post #10 of 10 (129 views)
Permalink
Re: [perl #114024] perl mistakenly warns when $] indexes a slice [In reply to]

On Wed, Aug 01, 2012 at 09:54:31PM +0200, Elizabeth Mattijsen wrote:
> > With a little bit of experimenting I did manage to find one difference:
> >
> > $ perl -le 'sub foo (\[$@]) {}; foo $a[0]'
> > $ perl -le 'sub foo (\[$@]) {}; foo @a[0]'
> > Type of arg 1 to main::foo must be one of [$@] (not array slice) at -e line 1, at EOF
> > Execution of -e aborted due to compilation errors.
>
> Wow, that's pretty obscure. I guess you *could* argue that that's a bug, but I won't :-)

There's also a context difference in the way the indices are calculated:

sub f { print wantarray, "\n"; 0 }
$a[f()] = 1;
@a[f()] = 1;

which gives

$ ./perl /tmp/p

1
$



--
You never really learn to swear until you learn to drive.

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.