Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of <=> (less than, equal, greater than) in Perl?

In this answer, I saw the syntax <=>; what does this mean? It seems to be some sort of comparison based on the context, but that's all I can gather. Partial context:

sub rev_by_date { $b->[9] <=> $a->[9] }
my @sorted_files = sort rev_by_date @files;
like image 830
jtpereyda Avatar asked Oct 21 '11 22:10

jtpereyda


1 Answers

From Perldoc:

Binary "<=>" returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument. If your platform supports NaNs (not-a-numbers) as numeric values, using them with "<=>" returns undef. NaN is not "<", "==", ">", "<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN returns true, as does NaN != anything else. If your platform doesn't support NaNs then NaN is just a string with numeric value 0.

like image 137
bhamby Avatar answered Oct 18 '22 22:10

bhamby