Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things


by gods
on Aug 24, 1999 at 22:42 UTC ( #241=perlfunc: print w/replies, xml ) Need Help??


See the current Perl documentation for eof.

Here is our local, out-dated (pre-5.6) version:

eof - test a filehandle for its end


eof ()


Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open. FILEHANDLE may be an expression whose value gives the real filehandle. (Note that this function actually reads a character and then ungetc()s it, so isn't very useful in an interactive context.) Do not read from a terminal file (or call eof(FILEHANDLE) on it) after end-of-file is reached. Filetypes such as terminals may lose the end-of-file condition if you do.

An eof without an argument uses the last file read as argument. Using eof() with empty parentheses is very different. It indicates the pseudo file formed of the files listed on the command line, i.e., eof() is reasonable to use inside a while (<>) loop to detect the end of only the last file. Use eof(ARGV) or eof without the parentheses to test EACH file in a while (<>) loop. Examples:

    # reset line numbering on each input file
    while (<>) {
        next if /^\s*#/;        # skip comments 
        print "$.\t$_";
    } continue {
        close ARGV  if eof;     # Not eof()!

    # 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

Practical hint: you almost never need to use eof in Perl, because the input operators return false values when they run out of data, or if there was an error.

Log In?

What's my password?
Create A New User
[MidLifeXis]: I have a user who has a lot of say on how some of our processes work that abhors significant blanks. Perhaps that is a part of it. A not-so-obvious "this space intentionally left blank" indicator.
[MidLifeXis]: On the back end, however, that doesn't mean that i have to use that value to indicate "undefined", no matter how much he thinks I should.
[stonecolddevin]: hey MidLifeXis, all
[MidLifeXis]: o/
[GotToBTru]: greets

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2017-01-20 19:18 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (176 votes). Check out past polls.