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