Perl UNLESS...ELSE Statement
An unless statement can be followed by an optional else statement, which executes when the boolean expression is true.
Syntax
The syntax is as follows:
unless(boolean_expression){
# Executed when the boolean expression boolean_expression is false
}else{
# Executed when the boolean expression boolean_expression is true
}
If the boolean expression boolean_expression is false, the code inside the unless block is executed. If the boolean expression is true, the code inside the else block is executed.
Flowchart
Example
#!/usr/bin/perl
$a = 100;
# Using unless statement to check the boolean expression
unless( $a == 20 ){
# Executed when the boolean expression is false
printf "The given condition is false\n";
}else{
# Executed when the boolean expression is true
printf "The given condition is true\n";
}
print "The value of a is : $a\n";
$a = "";
# Using unless statement to check the boolean expression
unless( $a ){
# Executed when the boolean expression is false
printf "The given condition for a is false\n";
}else{
# Executed when the boolean expression is true
printf "The given condition for a is true\n";
}
print "The value of a is : $a\n";
Executing the above program will output:
The given condition is false
The value of a is : 100
The given condition for a is false
The value of a is :