所以我在计算 Perl 在以下情况下的作用时遇到了一些问题:
while(1){
$inputLine=<STDIN>
#parse $inputLine below
#BUT FIRST, I need to check if $inputLine = EOF
}
在我得到使用
while(<>){}
的明显答案之前,让我说有一个非常充分的理由我必须执行上述操作(基本上是设置警报来中断阻塞,我不希望该代码使示例困惑)。有什么方法可以比较
$inputLine == undef
(因为我认为这就是 STDIN 最后返回的内容)。谢谢。
最佳答案
在你的循环中,使用
last unless defined $inputLine;
来自 perlfunc documentation on
defined
:defined EXPR
definedReturns a Boolean value telling whether EXPR has a value other than the undefined value
undef
. If EXPR is not present,$_
will be checked.Many operations return
undef
to indicate failure, end of file, system error, uninitialized variable, and other exceptional conditions. This function allows you to distinguishundef
from other values. (A simple Boolean test will not distinguish amongundef
, zero, the empty string, and"0"
, which are all equally false.) Note that sinceundef
is a valid scalar, its presence doesn't necessarily indicate an exceptional condition:pop
returnsundef
when its argument is an empty array, or when the element to return happens to beundef
.
关于perl - 如何在 Perl 中检查 eof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2195766/