Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Sorting result of function call

by jbert (Priest)
on Dec 20, 2006 at 09:45 UTC ( #590871=note: print w/ replies, xml ) Need Help??


in reply to Sorting result of function call

I don't know the details of what is going wrong, but I suspect that it is due to the attempts of sort to interpret it's first argument as a comparison sub or block.

If you put in an explicit { $a cmp $b } in as the first parameter, all of your tests do the same thing - print "called:array:13".

It's a bit ambiguous actually - should "sort subname" be interpreted as "using this sub as the sort comparison routine, sort the empty list" or "call this sub in list context and sort the results with the default sort comparator".

The behaviour is surprising on the face of it, but I'm not (yet) sure it's wrong. Yet another reason to eschew prototypes (in this case, that of sort) I suppose.


Comment on Re: Sorting result of function call
Download Code
Re^2: Sorting result of function call
by Hofmator (Curate) on Dec 20, 2006 at 09:58 UTC
    It's a bit ambiguous actually - should "sort subname" be interpreted as "using this sub as the sort comparison routine, sort the empty list" or "call this sub in list context and sort the results with the default sort comparator".

    Maybe I'm misunderstanding you here, but "sort subname" is my first testcase and it neither behaves as your first alternative nor as your second!

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Yes, I'd expect "sort subname" to mean "sort the empty list with the comparison func subname" - because "sort bareword LIST" means sort the (possibly empty) list with bareword as a comparison func.

      And I'd expect "sort subname()" to mean "invoke subname to get a list, to sort with the default comparator" - because I interpret "subname()" to always mean "invoke subname".

      So I'd expect your first two examples to have different behaviour (but not the behaviour we get).

      But I'm not sure my expectations are in line with "documented reality", so I'm loath to say there definitely is a bug.

      I'm hoping to learn something in this thread :-)

        There's no way for perl to perfectly satisfy here: sort subname LIST is clearly documented as using subname as the comparator in sorting LIST, and making the the optional whitespace before the '(' around a list become required would be unfortunate. But that leads to sort subname() not being interpreted as you intended. If you can train yourself to always use & on a sub call producing a list to be sorted, you'll be well off.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://590871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-07-30 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls