jtpereyda
jtpereyda

Reputation: 7385

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;

Upvotes: 11

Views: 15457

Answers (1)

bhamby
bhamby

Reputation: 15450

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.

Upvotes: 19

Related Questions