How can I check eof in Perl?

So, I have a little problem that determines what Perl does in the following case:

while(1){
$inputLine=<STDIN>

#parse $inputLine below
#BUT FIRST, I need to check if $inputLine = EOF

}

before I get the obvious answer to using while(<>){}, let me say that there is a very strong reason why I have to do the above (basically, set an alarm to interrupt the lock, and I did not want this code cluttering up the example).

Is it possible to compare $inputLine == undef(as I think, this is what STDIN returns at the end).

Thank.

+3
source share
3 answers

Inside your loop use

last unless defined $inputLine;

From the perlfunc documentation ondefined :

defined EXPR
is determined

, , EXPR , undefined undef. EXPR , $_.

undef, , , , . undef . ( undef, , "0", .) , undef , : pop undef, undef.

+11
defined($inputLine)

, . 4 select .

+4

eof . eof 1, FILEHANDLE EOF.

0
source

Source: https://habr.com/ru/post/1731283/


All Articles