sort with lexicographic order

cmp, lexicographic, perl, sorting

Solution

`or` is a short-circuit evaluator, so it will return the value of the left-hand side if it's true (which is any non-zero value), and otherwise will evaluate the right-hand side.

So in this case, if the animals' names compare as equal, (0 - false), the number of legs will be counted for sorting purposes.

Problem

I see the results from the following code, but I don't understand exactly how the `or` knows what to do in the following `sort` example: ``` use Data::Dumper; $animals{'man'}{'name'} = 'paul'; $animals{'man'}{'legs'} = 2; $animals{'cheeta'}{'name'} = 'mike'; $animals{'cheeta'}{'legs'} = 3; $animals{'zebra'}{'name'} = 'steve'; $animals{'zebra'}{'legs'} = 4; $animals{'cat'}{'name'} = ''; $animals{'cat'}{'legs'} = 3; $animals{'dog'}{'name'} = ''; $animals{'dog'}{'legs'} = 4; $animals{'rat'}{'name'} = ''; $animals{'rat'}{'legs'} = 5; @animals = sort { $animals{$a}{'name'} cmp $animals{$b}{'name'} or $animals{$a}{'legs'} <=> $animals{$b}{'legs'} } keys %animals; print Dumper(\@animals); ```

Original source