All the Perl that's Practical to Extract and Report

Navigation

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Without JavaScript enabled, you might want to
use the classic discussion system instead. If you login, you can remember this preference.

I’d say to skip sorting and return the number of elements efficiently. It would help the common case (return sort in scalar context), and I’ve never written a sort block with side effects, can’t imagine what it’d be useful for, or have seen any such thing in anyone else’s code, whether on PerlMonks or the CPAN.

I believe that unlike being undefined in scalar context, not sorting in scalar context is a kind of surprise that’s not very surprising, and if anyone really need

Someone's going to bring up side effects and point out that we're swapping one gotcha for another. The new gotcha being "sort in scalar context will not execute your code block, so if it has side effects they will not occur".

The defense to that is that side effects in sort are so rare that this is hardly a real world gotcha. Does anyone put side effects in their sort?

This is already part of the “sort in scalar context is undefined” gotcha.

Second – and as I’ve said –, I’ve never wanted to use side effects in a sort block, I can’t imagine a good reason that needs it, and I’ve never seen anyone else do it on PerlMonks or other fora.

For obvious reasons I won’t claim with certainty that noone does this. But I’d be willing to bet money that the number of instances where this would cause a problem are so vanishingly rare as to be ignorable.

Unfortunately the CPAN code search currently throws 500 Internal Server Error when searching on phrases, and I have no minicpan set up either. So someone else will have to look for hard numbers, if they’re necessary.