Perl-unless-statement
提供:Dev Guides
Perlのexceptステートメント
Perl unless ステートメントは、ブール式とそれに続く1つ以上のステートメントで構成されます。
構文
Perlプログラミング言語のunless文の構文は次のとおりです-
unless(boolean_expression) {
# statement(s) will execute if the given condition is false
}
ブール式の評価が false の場合、unlessステートメント内のコードブロックが実行されます。 ブール式が true と評価された場合、unlessステートメントの終了後(閉じ中括弧の後)の最初のコードセットが実行されます。
数値0、文字列 '0’および ""、空のリスト()、およびundefはすべてブールコンテキストで false であり、他のすべての値は true です。 !*または *not による真の値の否定は、特別な偽の値を返します。
流れ図
例
#!/usr/local/bin/perl
$a = 20;
# check the boolean condition using unless statement
unless( $a < 20 ) {
# if condition is false then print the following
printf "a is not less than 20\n";
}
print "value of a is : $a\n";
$a = "";
# check the boolean condition using unless statement
unless ( $a ) {
# if condition is false then print the following
printf "a has a false value\n";
}
print "value of a is : $a\n";
最初にステートメントが小なり演算子(<)を使用しない限り、2つのオペランドを比較し、最初のオペランドが2番目のオペランドより小さい場合はtrueを返し、そうでない場合はfalseを返します。 したがって、上記のコードが実行されると、次の結果が生成されます-
a is not less than 20
value of a is : 20
a has a false value
value of a is :