Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • It would really help if sort would take a subroutine reference but it doesn't it takes a block or a subroutine name. Grump.

    Thankfully, you're wrong. sort is happy to take a code ref.

    @x = 1 .. 20;
    $bynumdesc = sub { return $b <=> $a };
    $\ = "\n";
    print for sort $bynumdesc @x;

    That definitely would appear to work, correctly printing the numbers from 1 to 20 in reversed order, on any perl I throw it at, from 5.6.1 to 5.10.0.

    • Hmmm, interesting. It takes a coderef if that coderef is stored in a variable, but not just a plain old code-ref, or one returned directly from a function. I suppose it's not entirely unreasonable for the parser to get a bit confused in that case.
      • Yeah, the sort function has a bunch of terrible, horrible, awful special cases in place that are supposed to DWIM but end up surprising me much of the time I use sort otherwise than by passing an explicit, inline comparator block.