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 format is as follows:
unless(boolean_expression){
# 在布尔表达式 boolean_expression 为 false 执行
}else{
# 在布尔表达式 boolean_expression 为 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
Example
#!/usr/bin/perl
$a = 100;
# Use the unless statement to test the boolean expression
unless( $a == 20 ){
# Executes when the boolean expression is false
printf "The given condition is false\n";
}else{
# Executes when the boolean expression is true
printf "The given condition is true\n";
}
print "The value of a is :$a\n";
$a = "";
# Use the unless statement to test the boolean expression
unless( $a ){
# Executes when the boolean expression is false
printf "The given condition for a is false\n";
}else{
# Executes 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 produces the following output:
给定的条件为 false a 的值为 : 100 a 给定的条件为 false a 的值为 :Other Extensions
Perl Conditional Statements