Ternary operator  ?? !!

The ?? !! operator is the only ternary operator in Perl 6. It is also called the conditional operator. It takes three operands—a condition and two values. If the condition is evaluated as True, the second operand is returned as a result of the operation. Otherwise, the third operand is returned.

say pi < 3 ?? 'Less than 3' !! 'More than 3';

In this example, the pi < 3 condition is False, so the second string More than 3 is printed.

The ternary operator can (with care) be used to test more than one condition. Consider the following example:

my $value = rand;say $value;say $value < 0.3 ?? '0.0 to 0.3' !! $value < 0.5 ?? '0.3 to 0.5' !! $value < 0.7 ?? '0.5 to 0.7' !!                 '0.7 to 1.0';

In such cases, the formatting of the ...

Get Perl 6 Deep Dive now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.