September 2017
Beginner
402 pages
9h 52m
English
Now let us see how we can understand if something wrong has happened. In Perl 6, there is a special variable, $!, called the error variable. It contains an exception if it happened. Let us use it to check whether everything is OK:
my $a = prompt 'Enter dividend > ';
my $b = prompt 'Enter divisor > ';
try {
my $c = $a / $b;
say "The result of $a / $b is $c.";
}
if $! {
say 'Failure!';
}
else {
say 'All fine.';
}
say 'Done.';
We added the test if $!. It treats the $! variable in a Boolean context. Actually, the value of the variable in the presence of an exception is a value of the Exception type. We will see it later in this chapter in the The Exception object section. For now, it is enough to know that in a Boolean context ...
Read now
Unlock full access