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

11,770

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.

Share:
11,770
jtpereyda
Author by

jtpereyda

Maintainer of boofuzz. "If you're going to make a backward-compatibility-breaking change, no time is better than now; things will be worse in the future." Eric Lippert Sharp Regrets: Top 10 Worst C# Features

Updated on June 05, 2022

Comments

  • jtpereyda
    jtpereyda almost 2 years

    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;
    
  • jtpereyda
    jtpereyda over 12 years
    Thank you, that link will be helpful too!