Perl UNLESS statement

Perl UNLESS statement Perl conditional statement

An unless statement consists of a Boolean expression followed by one or more statements.

Grammar

The syntax format looks like this:

unless(boolean_expression){
   # 在布尔表达式 boolean_expression 为 false 执行
}

If the Boolean expression boolean_expression false, the block of code within the if statement is executed. If the Boolean expression is true, the first set of code (after parentheses) at the end of the if statement is executed.

Flow chart

Perl UNLESS statement

Instance

#!/usr/bin/perl

$a = 20;
# 使用 unless 语句检测布尔表达式
unless( $a < 20 ){
    # 布尔表达式为 false 时执行
    printf "a 大于等于 20\n";
}
print "a 的值为 : $a\n";

$a = "";
# 使用 unless 语句检测布尔表达式
unless ( $a ){
    # 布尔表达式为 false 时执行
    printf "条件 a 为 false\n";
}
print "a 的值为 : $a\n";

The above procedure is performed and the output is:

a 大于等于 20
a 的值为 : 20
条件 a 为 false
a 的值为 :

Perl UNLESS statement Perl conditional statement