perl中,0,"","0"代表false。
||用于测试变量是否为false或未定义,defined则用于测试变量是否未定义,而不是false。
$foo = $bar || "DEFAULT VALUE";
print "$foo\n"; # 输出"DEFAULT VALUE"
$bar="0";
$foo = $bar || "DEFAULT VALUE";
print "$foo\n"; # 输出"DEFAULT VALUE"
$bar="";
$foo = $bar || "DEFAULT VALUE";
print "$foo\n"; # 输出"DEFAULT VALUE"
$bar=0;
$foo = $bar || "DEFAULT VALUE";
print "$foo\n"; # 输出"DEFAULT VALUE"
$foo = defined($bar1) ? $bar1 : "DEFAULT VALUE";
print "$foo\n"; # 输出"DEFAULT VALUE"
$bar1="0";
$foo = defined($bar1) ? $bar1 : "DEFAULT VALUE";
print "$foo\n"; # 输出"0"
$bar1="";
$foo = defined($bar1) ? $bar1 : "DEFAULT VALUE";
print "$foo\n"; # 输出""
$bar1=0;
$foo = defined($bar1) ? $bar1 : "DEFAULT VALUE";
print "$foo\n"; # 输出"0"
阅读(486) | 评论(0) | 转发(0) |