mce has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

There are many posts about the magic of sort.

I have a question that -I think- isn't handled before.

Last week, I found that the result of my code was not correct, and after digging deep in my code, I found that the problem was a typo, as I have written cmp instead of <=>.

This bug is one of the hardest to find as the perl code runs OK.

Now, when warnings are on, perl complains that the following code is wrong.

perl -W -e 'warn sort {$a <=> $b} qw(A B);'
but this isn't.
perl -W -e 'warn sort {$a cmp $b} qw(1 2);'
Wouldn't it be possible to alter perl so that it complains when using cmp when comparing only numbers?
At least, I would think this is very usefull.
I don't know that much about the perl internals, but I guess it must be doable.
Dr. Mark Ceulemans
Senior Consultant
BMC, Belgium

Replies are listed 'Best First'.
Re: yet another sort issue
by tilly (Archbishop) on Jun 23, 2003 at 11:51 UTC
    It is possible to do this. However it would result in impossible to avoid warnings in the common case where someone is sorting a list which has a mix of numbers and strings. This would be annoying enough that you are unlikely to get general agreement on adding your warning. As well it would slow things down because you force cmp to study whether or not strings look like numbers, rather than just going ahead and comparing them.

    By contrast the warning you are comparing it to will catch any time when Perl has to use a string as a number and doesn't think that it looks like a number. It shows up for any numerical operator, not just <=>. This warning is far more useful because if it shows up, you almost certainly are doing something wrong. And the performance penalty is negligable because the operation that you have to do to test for the error is an operation that you are already doing (namely test the string to see if it has any reasonable way of being interpreted as a number).

    If you still want to argue for it, use the perlbug utility to send your suggestion to the core Perl developers. Do not be dismayed if they reject it.

Re: yet another sort issue
by Tomte (Priest) on Jun 23, 2003 at 11:46 UTC

    You should probably read up what qw is doing:

    perl -MData::Dumper -e 'print Dumper([qw(1 2)]);'; $VAR1 = [ '1', '2' ];
    perl -MData::Dumper -e 'print Dumper([1,2]);'; $VAR1 = [ 1, 2 ];

    You are in fact comparing strings :), but that doesn't matter.
    As numbers are always completly valid as string-value and the internal conversion is almost always what you want, this is maybe one if the times where DWIM instead of DWIT is biting you in the privat parts...


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: yet another sort issue
by zby (Vicar) on Jun 23, 2003 at 11:41 UTC
    In both examples you use strings as qw(1 2) means a list of strings. To have a list of numbers use (1, 2). By the way on my system I don't get any warning of that kind with any of the possibillities and I have a 5.8.0 on a cygwin system.