The bug in the module is that it is not prepared to handle a sort routine that can receive 2 distinct arguments and return 0. If you enable a last comparison in your sort routine that just does $Sort::External::a cmp $Sort::External::b then you won't get an endless loop. Alternately, assuming that Perl is using a stable sort (it does by default I believe, but that's not a safe default) you can fix the bug with the following patch to Sort/External.pm:
--- External.pm.bak 2008-07-11 22:30:52.000000000 -0700 +++ External.pm 2008-07-11 22:52:14.000000000 -0700 @@ -322,7 +322,7 @@ $comparecode = sub { my $test = shift; return 0 if $test eq $cutoff; - my $winner = ( sort $sortsub( $cutoff, $test ) )[0]; + my $winner = ( sort $sortsub( $test, $cutoff ) )[0]; return $winner eq $cutoff ? 1 : -1; }; }
While I'm at it I should mention that you have a lot of unnecessary checks in your comparison. You're writing
# .... return -1 if $hours1 < $hours2; return 1 if $hours1 > $hours2; return -1 if $hours1 == $hours2 && $minutes1 < $minutes2; return 1 if $hours1 == $hours2 && $minutes1 > $minutes2; return -1 if $hours1 == $hours2 && $minutes1 == $minutes2 && $sec +onds1 < $seconds2; return 1 if $hours1 == $hours2 && $minutes1 == $minutes2 && $seco +nds1 > $seconds2;
but if $hours1 and $hours2 are different then you'll never get to the minutes comparison. So you can get rid of those and just have:
# ... return -1 if $hours1 < $hours2; return 1 if $hours1 > $hours2; return -1 if $minutes1 < $minutes2; return 1 if $minutes1 > $minutes2; return -1 if $seconds1 < $seconds2; return 1 if $seconds1 > $seconds2;
But you can write this more idiomatically using the fact that || short circuits:
return $hours1 <=> $hours2 || $minutes1 <=> $minutes2 || $seconds1 <=> $seconds2 || $Sort::External::a cmp $Sort::External::b;
Incidentally in one of the few useful uses of prototypes in Perl, if a sort sub has a prototype of ($$) then the arguments are passed in as @_ and not $a, $b. So if you write
my $sortscheme = sub ($$) { my @fieldsa = split(" ", shift); my @fieldsb = split(" ", shift); # ... };
then you don't have to worry about what package the sort is coming from.

Oh and a final tip? You should use strict.pm. Always.


In reply to Re: Problem with Sort::External 0.16 by tilly
in thread Problem with Sort::External 0.16 by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.