[sf-perl] Sort subroutine didn't return single value at -e line 1.

George Hartzell hartzell at alerce.com
Fri Apr 5 14:44:32 PDT 2013


Bill Moseley writes:
 > Could someone explain what's happening here with sort?
 > 
 > 5.14.2:
 > 
 > $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
 > [uniq 3,1,3,2]'
 > $VAR1 = [
 >           3,
 >           1,
 >           2
 >         ];
 > 
 > Might be nice to sort those -- like one would do in a shell:
 > 
 > 
 > $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
 > [sort uniq 3,1,3,2]'
 > $VAR1 = [
 >           1,
 >           2,
 >           3,
 >           3
 >         ];
 > 
 > 
 > Got an extra value there?
 > 
 > 
 > That code above was in one of our unit tests, and when I ran with 5.16.1 I
 > then get:
 > 
 > 
 > $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
 > [sort uniq 3,1,3,2]'
 > Sort subroutine didn't return single value at -e line 1.
 > 
 > 
 > This works, though:
 > 
 > $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
 > [uniq sort 3,1,3,2]'
 > $VAR1 = [
 >           1,
 >           2,
 >           3
 >         ];

It seems to be using 'uniq' as it's sorting block.

This works

      perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print \
        Dumper [sort {$a <=> $b} uniq (3,1,3,2)]'

Not sure *why* yet though.

g.



More information about the SanFrancisco-pm mailing list