Operators

Comparison & chaining

Full

Numeric vs string comparison, the three-way <=> / cmp, and chained relations.

Comparison operators come in two families: numeric (==, !=, <, <=, >, >=) and string (eq, ne, lt, le, gt, ge). Choosing by operator, not by value, keeps intent explicit.

Numeric vs string equality #

say 5 == 5.0;
say "5" eq "5.0";
say "a" eq "a";
Output
True
False
True

5 == 5.0 is True (equal as numbers); "5" eq "5.0" is False (different as text).

Three-way comparison — <=> and cmp #

<=> compares numerically, cmp compares in a type-sensible way (numbers numerically, strings alphabetically). Both return Less, Same, or More.

say 3 <=> 5;
say "a" cmp "b";
Output
Less
Less

Chained comparisons #

Relations chain the way they do in mathematics: 1 < 2 < 3 means 1 < 2 and 2 < 3, and each operand is evaluated once.

say 1 < 2 < 3;
say 1 < 5 < 3;
Output
True
False

Notes #