in reply to Re^2: Problem with a sort result
in thread Problem with a sort result

Perl often defers to C, or underlying platform, when it comes to specifications. (Sort of a hand-wave.) This seems to be the case here, as well. Read the page on qsort and compare to sort. Here's the pertinent quote:

If SUBNAME is specified, it gives the name of a subroutine that returns an integer less than, equal to, or greater than 0, depending on how the elements of the list are to be ordered.

So the behaviour is documented, and there is a discrepency in perl. You could try to fix the perl sources to address this issue. (But be sure then to run all tests. I've no clue why it's coded that way; there might be a reason.)

The following patch applies to one location only, and there are half a dozen more. Just a demonstration.

--- perl-5.22.1/pp_sort.c.orig 2015-10-17 12:38:38.000000000 +0000 +++ perl-5.22.1/pp_sort.c 2016-01-27 15:05:18.000000000 +0000 @@ -1768,11 +1768,11 @@ static I32 S_sortcv(pTHX_ SV *const a, SV *const b) { const I32 oldsaveix = PL_savestack_ix; const I32 oldscopeix = PL_scopestack_ix; - I32 result; + IV result; SV *resultsv; PMOP * const pm = PL_curpm; OP * const sortop = PL_op; COP * const cop = PL_curcop; @@ -1801,11 +1801,11 @@ while (PL_scopestack_ix > oldscopeix) { LEAVE; } leave_scope(oldsaveix); PL_curpm = pm; - return result; + return (result > 0) - (result < 0); }