Yet Another Perl 6 Operator: Comparisons - Part I


Maintainer: Adriano Ferreira <>
Date: 28 Sep 2007
Last Modified: 28 Sep 2007
Number: 5
Version: 2
Status: Published


As expected, Perl 6 supports the usual comparison operators. This includes the numeric comparison operators:

== != < <= > >=

(where '!=' is a short for '!==', the negated version of '=='). These operators convert their terms into numbers before comparison.

The string comparisons operators are here as well.

eq ne lt le gt ge

And, just like in Perl 5, they coerce to strings their operands before comparison. 'ne' is short for '!eq'.

With numeric and string comparison operators in Perl 5, one must artificially choose one of these sets (or both) to overload if some generic comparison semantics was needed for certain objects. Perl 6 introduces two new operators for doing these generic comparisons:

$o1 after $o2
$o1 before $o2

These force no coercion, being suitable to express user-defined ordering of objects.

A nice feature of the comparison operators is that they all may be chained. Thus, Perl 6 supports a natural extension for comparison expressions with multiple operands:

if 1 < $a < 100 { say "Good, you picked a number *between* 1 and 100." }

if 3 < $roll <= 6 { say "High roll" }

if 1 <= $roll1 == $roll2 <= 6 { print "Doubles!" }

if $s1 le $s2 le $s3 { say "Triple of strings is ordered" }

A chain of comparisons short-circuits: meaning that if a comparison fails, the remaining comparisons to the left never evaluate.

1 > 2 > die("this is never reached");

Each argument in the chain will evaluate at most once:

1 > $x++ > 2   # $x increments exactly once

$Revision: 45 $