5> 6的返回值是undef。但是,當我將5> 6的值分配給定義變量的變量時。如何使用比較運算符來傳遞語句的值,該比較運算符在失敗時解析爲undef?分配給變量的Undef值。
#!/usr/bin/perl
use strict ;
use warnings;
print'Five is more than six ? ', 5 > 6, "\n";
print 'Five is less than six ? ' , 5 < 6 , "\n";
my $wiz = 5 > 6 ;
if (defined($wiz)) {
print '$wiz is defined' ;
} else {
print '$wiz is undefined' ;
}
$ ./lessthan
Five is more than six ?
Five is less than six ? 1
$wiz is defined
這不是空字符串,它是一個雙變量。它用作字符串時用作空字符串,用作數字時用作零。請參閱http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood和http://perldoc.perl.org/perlop.html#Relational-Operators – ThisSuitIsBlackNot
因此,小於或大於運算符會解析爲1在成功和「falsey」失敗,這是一個空字符串,而不是undef。 – capser
我只是看到結果關閉5> 6沒有什麼,或undef。因此,如果我將5> 6的結果賦予變量,你會認爲5> 6的結果似乎沒有被定義,但是通過賦值給變量 - 你可以定義它。 – capser