An unless statement can be followed by an optional else statement, which is executed when the Boolean expression is true.
The syntax format is as follows:
unless(boolean_expression){ # execute when boolean expression boolean_expression is false}else{ # execute when boolean expression boolean_expression is true}
If the boolean expression boolean_expression is false, the code within the unless block is executed. If the Boolean expression is true, the code within the else block is executed.
#!/usr/bin/perl$a = 100;# Use the unless statement to detect the Boolean expression unless( $a == 20 ){ # When the Boolean expression is false, printf "The given condition is falsen"; }else{ # When the Boolean expression is true, execute printf "The given condition is truen";}print "The value of a is: $an";$a = "";# Use unless Statement detects Boolean expression unless( $a ){ # When the Boolean expression is false, execute printf "a The given condition is falsen";}else{ # When the Boolean expression is true, execute printf "a The given condition is truen";}print "The value of a is: $an";
Executing the above program, the output result is:
The given condition is false and the value is: 100a The given condition is false and the value is: