Perl UNLESS Statement
An unless statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax is as follows:
unless(boolean_expression){
# Executed when boolean_expression is false
}
If the boolean expression boolean_expression is false, the code block within the unless statement will be executed. If the boolean expression is true, the first set of code after the closing bracket will be executed.
Flowchart
Example
#!/usr/bin/perl
$a = 20;
# Using unless statement to check boolean expression
unless( $a < 20 ){
# Executed when boolean expression is false
printf "a is greater than or equal to 20\n";
}
print "The value of a is : $a\n";
$a = "";
# Using unless statement to check boolean expression
unless ( $a ){
# Executed when boolean expression is false
printf "Condition a is false\n";
}
print "The value of a is : $a\n";
Executing the above program will output:
a is greater than or equal to 20
The value of a is : 20
Condition a is false
The value of a is :