让我们检查以下perl代码
if ($a lt 0.00 or $a gt 100.000)
{
print "a must be between 0 and 100 \n";
exit 1
}
exit 0
Run Code Online (Sandbox Code Playgroud)
让我们说a等于5.上面的代码将以失败状态退出,因为a不在0到100之间.
只需简单地更换lt,并gt与他们所代表的实际经营者<,并>分别产生预期的结果.用以9开头的数字替换100也将产生预期结果.
为什么Perl的比较运算符告诉我5不在0到100之间?
在perl中lt,gt运算符与<and不同>.perl文档详细说明了这里的perlop在理性运算符下,下面是从文档中提取的:
Binary "<" returns true if the left argument is numerically less than the right argument.
Binary ">" returns true if the left argument is numerically greater than the right argument.
Binary "<=" returns true if the left argument is numerically less than or equal to the right argument.
Binary ">=" returns true if the left argument is numerically greater than or equal to the right argument.
Binary "lt" returns true if the left argument is stringwise less than the right argument.
Binary "gt" returns true if the left argument is stringwise greater than the right argument.
Binary "le" returns true if the left argument is stringwise less than or equal to the right argument.
Binary "ge" returns true if the left argument is stringwise greater than or equal to the right argument.
Run Code Online (Sandbox Code Playgroud)
由于perl没有字符串对象,因此整数对象perl必须猜测对象的上下文.perl的唯一方法是知道你是在比较一个字符串还是一个整数,这是通过确保理性运算符lt和gt强制比较的上下文作为一个sting和那个<和>操作符来进行比较作为整数的比较