develooper Front page | perl.perl5.porters | Postings from August 2019

Antw: [perl #134312] wrong line number reported for sort subroutine?(Argument "a" isn't numeric in numeric comparison (<=>) at ... line ...)

Thread Next
From:
Ulrich Windl
Date:
August 6, 2019 07:32
Subject:
Antw: [perl #134312] wrong line number reported for sort subroutine?(Argument "a" isn't numeric in numeric comparison (<=>) at ... line ...)
Message ID:
5D492B96020000A100032BB6@gwsmtp.uni-regensburg.de
>>> "Tony Cook via RT" <perlbug-followup@perl.org> schrieb am 06.08.2019 um 08:16
in Nachricht <rt-4.0.24-9652-1565072215-957.134312-94-0@perl.org>:
> On Thu, 25 Jul 2019 01:20:48 -0700, Ulrich.Windl@rz.uni-regensburg.de wrote:
>> Hi!
>> 
>> I think this is a bug in perl 5.18.2 (as shipped in SLES12 SP4):
>> 
>> Debugging a sort routine I wrote myself I get an error that I don't
>> understand, most likely because the line number reported (maybe even
>> the whole error message) is wrong:
> 
> Could your sort comparison block be returning "" ?

It could be that some if..else branch was incomplete and did not return an explicit value.
Actually what I did in the meantime was to convert the block to use a subroutine. Then the line numbers were correct, and I was able to complete the routine.

The code now looks like this:
# generic comparer
sub generic_cmp($)
{
    my $num_re = shift;

    return sub ($$) {
        my ($a, $b) = @_;
        my $result = $a cmp $b;

        while ($result != 0) {
...
        }
        return $result;
    };
}

sub generic_sort(@)
{
    my $cmp = generic_cmp(RE_UNSIGNED_INT);

    return sort { $cmp->($a, $b) } @_;
}

Note: It may be less efficient, but at least it works and is debuggable ;-)

> 
> $ perl5.18.3 -wle 'my @x = sort { "" } qw(1 2 3)'
> Argument "" isn't numeric in sort at -e line 1.
> 
> Tony

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