Taking decisions based on conditions is one of the fundamental needs in programming. The if keyword changes the flow of the program, depending on the result of a Boolean test. Considering the following code:
my $x = 5;
if $x < 10 {
say "$x < 10"; # 5 < 10
}
In this example, you can see the syntax used with the if keyword. The keyword is followed by a Boolean condition $x < 10, followed by the block of code in the curly braces. Unlike Perl 5, parentheses around the condition are not necessary.
The block of code is only executed if the condition evaluates to True.
The if statement can be accomplished by the else branch, which will take control when the condition is False:
my $x = 11;
if $x < 10 {
say "$x < 10";
}
else {
say "$x >= 10"; # 11 >= 10
}
With the given value of $x, the program executes the code...