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

[perl #86136] strange inconsistency in detecting Can't use "my $b" in sort comparison

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
July 3, 2012 21:45
Subject:
[perl #86136] strange inconsistency in detecting Can't use "my $b" in sort comparison
Message ID:
rt-3.6.HEAD-28836-1341377095-10.86136-15-0@perl.org
On Tue Jun 05 09:27:47 2012, sprout wrote:
> On Mon Mar 14 09:22:34 2011, nicholas wrote:
> > This one makes no sense to me:
> > 
> > $ cat eval.pl
> > #!./perl
> > 
> > use strict;
> > use warnings;
> > 
> > my $b = 'wrong';
> > 
> > my @a = sort
> >     { print; $a <=> $b }
> >     2, 1;
> > 
> > BEGIN {
> >     print "Got here\n";
> > }
> > 
> > my @b = sort
> >     { func(); $a <=> $b }
> >     2, 1;
> >  ./perl -Ilib eval.pl
> > Got here
> > Can't use "my $b" in sort comparison at eval.pl line 17.
> > 
> > 
> > Why does the error only trigger on the second?
> > It's not because it's second - its because *some* things cause the
> >    code in
> > toke.c that generates the parsing error not to be reached.
> > 
> > Editing the code a bit reveals that the $b *is* being bound to the
> >    lexical.
> > 
> > It doesn't seem to be based on the class or arity of the builtin -
> >    print and
> > sleep do it, endprotoent does not.
> > 
> > I have no clue what is going on here.
> 
> That code in toke.c is simply horrible.  It does a simple scan for <=>
> or cmp anywhere on the same ‘line’.  But I don’t fully understand how
> ‘line’ is determined.
> 
> $ perl -e 'my $a; sort { ; } $a <=> $b'
> Can't use "my $a" in sort comparison at -e line 1.
> 
> $ perl -e 'my $a; sort { ; } $a, "<=>"'
> Can't use "my $a" in sort comparison at -e line 1.
> 
> $ perl -e 'my $a; sort { ; } $a, $cmp'
> Can't use "my $a" in sort comparison at -e line 1.
> 
> This error has happened to me before, and I found it extremely annoying.
>  It should probably be downgraded to a warning.

I’ve downgraded it to a warning and reimplemented it in op.c, so it no
longer gets confused by the presence of the magic sequences ‘cmp’ and ‘<=>’.

By the message still says "my $a", even for state variables.  That’s
something I didn’t fix.

The warning is now ‘"my $a" used in sort comparison’.

Should I use "state $a" for state variables, or just say ‘Lexical
variable $a’?

-- 

Father Chrysostomos


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

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