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

Mailing List Archive: Perl: porters

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

 

 

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


perlbug-followup at perl

Jul 3, 2012, 9:44 PM

Post #1 of 2 (60 views)
Permalink
[perl #86136] strange inconsistency in detecting Can't use "my $b" in sort comparison

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


perlbug-followup at perl

Jul 4, 2012, 9:10 AM

Post #2 of 2 (55 views)
Permalink
[perl #86136] strange inconsistency in detecting Can't use "my $b" in sort comparison [In reply to]

On Tue Jul 03 21:44:56 2012, sprout wrote:
> 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
‘<=>’.

I forget to mention it was 271c8bde9d.

And the warning now occurs when $a or $b is an operand to <=> or cmp and
the <=> or cmp is the top-level op of the last statement in the block.

>
> 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’?

I changed it to "state $a" in commit a2e39214970.

--

Father Chrysostomos


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

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.