perl - 如何在 Perl 中检查 eof?

标签 perl eof

所以我在计算 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
defined

Returns 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 distinguish undef from other values. (A simple Boolean test will not distinguish among undef, zero, the empty string, and "0", which are all equally false.) Note that since undef is a valid scalar, its presence doesn't necessarily indicate an exceptional condition: pop returns undef when its argument is an empty array, or when the element to return happens to be undef.

关于perl - 如何在 Perl 中检查 eof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2195766/

相关文章:

perl - Perl 中的 “grep -n” 是什么?

Erlang:如何将 stdin 输入从文件传输到 erlang 程序并匹配 eof?

c - *可能* unsigned char 等于 EOF?

c++ - 带 POST 的 curl_easy_perform 等待 EOF

c - strtok() C 字符串到数组

启用 javascript 的按钮在 IE 中不正确发布

Perl 执行带有计数行的 pgrep

perl - 将 MD5/SHA1 哈希从二进制转换为十六进制摘要

javascript - 在具有相同类的情况下,单击事件仅触发一次

java - 读取 EOF 后使用 Scanner 类读取 Standard.In 时出现 NoSuchElementException