cazz has asked for the wisdom of the Perl Monks concerning the following question:

I have run into an odd sorting issue when sorting an empty set. When you sort an empty list returned from a function, data leaks into a sort call.
use Data::Dumper; sub foo { return (); } warn Dumper(sort foo('1000'));
You would expect that Dumper would print an empty list. However, I get the following:
$VAR1 = '1000';

This looks like a bug to me but wanted to get more eyes on it first before I added a bug. I've checked both supersearch and perl's bug database without finding a prior report of this issue.

Replies are listed 'Best First'.
Re: leaking into sort when sorting an empty set
by ikegami (Patriarch) on Mar 21, 2006 at 16:08 UTC

    sort foo('1000');
    is the same as
    sort foo ('1000');
    is the same as
    sort foo '1000';
    In all of the above, foo is used as the compare function to sort the list consisting of ('1000').

    Specifiy a compare function explicitely
    sort { $a cmp $b } foo('1000');
    or add parenthesises around the arguments to skip the magic
    sort(foo('1000'));

Re: leaking into sort when sorting an empty set
by tilly (Archbishop) on Mar 21, 2006 at 16:10 UTC
    Try this:
    use Data::Dumper; sub foo { return (); } warn Dumper(sort {$a <=> $b} foo('1000'));
    and you get what you expect.

    Or try:

    use Data::Dumper; sub foo { return (); } warn Dumper(sort(foo('1000')));
    It looks like Perl thinks that foo is the sort function, and 1000 is what is being passed into the sort. Both those solutions disambiguate the situation.