eof 
eof FILEHANDLE
eof()
eofThis function returns true if the next read on
FILEHANDLE would return end-of-file or if
FILEHANDLE is not open.
FILEHANDLE may be an expression whose value
gives the real filehandle or a reference to a filehandle object of some
sort. An eof without an argument
returns the end-of-file status for the last file read. An eof() with empty parentheses () tests the ARGV filehandle (most commonly seen as the
null filehandle in <>).
Therefore, inside a while (<>)
loop, an eof() with parentheses will
detect the end of only the last of a group of files. Use eof (without parentheses) to test
each file in a while
(<>) loop. For example, the following code inserts
dashes just before the last line of the last
file:
while (<>) {
if (eof()) {
say "–" x 30;
}
print;
}On the other hand, this script resets line numbering on each input file:
# reset line numbering on each input file
while (<>) {
next if /^\s*#/; # skip comments
print "$.\t$_";
} continue {
close ARGV if eof; # Not eof()!
}Like “$” in a sed program, eof tends to show up in line number ranges.
Here’s a script that prints lines from /pattern/ to the end of each input
file:
while (<>) {
print if /pattern/ .. eof;
}Here, the flip-flop operator (..) evaluates the pattern match for each line. Until the pattern matches, the operator returns false. When it finally matches, the operator starts returning true, causing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access