Another article of the series “Yet Another Perl 6 Operator”
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
Wait for corrections and updates, as there was no time to submit this article for review. Some examples were copied verbatim from Synopsis 03.
Next article will be due Monday (October 1, 2007).
LINKS
- Synopsis S03, the official source
- The introduction of this series
- Official Perl 6 Documentation
- Perl 6 in your browser


AWESOME... this is great stuff.. awesome article.. I was really hoping that perl6 would address some issues like this one. Chaining is SO much easier to read than the endless Or's and And's cluttering up my code..
Would it be possible for you to keep the introduction webpage up-to-date with links to all the other entries in this series? I'd like to have one page I can bookmark to see whether a new installment has been added.
@Ben
I am feebly trying to follow a schedule. But you're right and, from now on, publishing of articles may be delayed but they will be announced to get some feedback before going live.
@Joe
Ok, I will keep the index in the introduction webpage up-to-date. I am glad that it (apparently) stopped changing the URL after updates (which is some odd quirky due to MovableType).
Pardon my nitpicking, but I think this statement needs to be corrected. Changing the word left to right makes sense in light of the example given.