Cheap web site hosting service by

 Back to Index


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 ungetcs 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. File types such as terminals may lose the end-of-file condition if you do.

An eof without an argument uses the last file read. Using eof() with empty parentheses is very different. It refers to the pseudo file formed from the files listed on the command line and accessed via the <> operator. Since <> isn't explicitly opened, as a normal filehandle is, an eof() before <> has been used will cause @ARGV to be examined to determine if input is available. Similarly, an eof() after <> has returned end-of-file will assume you are processing another @ARGV list, and if you haven't set @ARGV, will read input from STDIN; see perlop/"I/O Operators".

In a while (<>) loop, eof or eof(ARGV) can be used to detect the end of each file, eof() will only detect the end of the last file. 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 last; is needed if we
				# are reading from the terminal

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




Domain registration - Domain registration service and cheap domain transfer

Cheap domain name:
Register domain name services from just
$8.95/year only

Register domain name search -
Register domain or buy domain name registration and cheap domain transfer at low, affordable price.

© 2002-2004 Web Site Hosting Service


[ Don't get suckered in by the comments— they can be terribly misleading. Debug only code.   ]



Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to