perlfunc
gods
<P>
eof - test a filehandle for its end
<P>
<HR>
<P>
eof
<FONT SIZE=-1>FILEHANDLE</FONT>
<P>
eof ()
<P>
eof
<P>
<HR>
<P>
Returns 1 if the next read on
<FONT SIZE=-1>FILEHANDLE</FONT> will return end of file, or if
<FONT SIZE=-1>FILEHANDLE</FONT> is not open.
<FONT SIZE=-1>FILEHANDLE</FONT> may be an expression whose value gives the real filehandle. (Note that this function actually reads a character and then
<CODE>ungetc()</CODE>s it, so isn't very useful in an interactive context.) Do not read from a
terminal file (or call
[perlfunc:eof|eof(FILEHANDLE)] on it) after end-of-file is reached. Filetypes such as terminals may lose
the end-of-file condition if you do.
<P>
An [perlfunc:eof|eof] without an argument uses the last file read as argument. Using [perlfunc:eof|eof()] with empty parentheses is very different. It indicates the pseudo file
formed of the files listed on the command line, i.e., [perlfunc:eof|eof()] is reasonable to use inside a <CODE>while (<>)</CODE> loop to detect the end of only the last file. Use [perlfunc:eof|eof(ARGV)] or eof without the parentheses to test
<EM>EACH</EM> file in a while (<>) loop. Examples:
<P>
<PRE> # reset line numbering on each input file
while (<>) {
next if /^\s*#/; # skip comments
print "$.\t$_";
} continue {
close ARGV if eof; # Not eof()!
}
</PRE>
<P>
<PRE> # insert dashes just before last line of last file
while (<>) {
if (eof()) { # check for end of current file
print "--------------\n";
close(ARGV); # close or break; is needed if we
# are reading from the terminal
}
print;
}
</PRE>
<P>
Practical hint: you almost never need to use [perlfunc:eof|eof] in Perl, because the input operators return false values when they run out
of data, or if there was an error.
<HR>