Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Raise an exception or bail outReturn file descriptor from fi ... (Berikutnya)
Input and output functions

Test a filehandle for its end

Daftar Isi

  • eof FILEHANDLE

  • eof ()
  • eof

    Returns 1 if the next read on FILEHANDLE will return end of file or ifFILEHANDLE is not open. FILEHANDLE may be an expression whose valuegives the real filehandle. (Note that this function actuallyreads a character and then ungetcs it, so isn't useful in aninteractive context.) Do not read from a terminal file (or calleof(FILEHANDLE) on it) after end-of-file is reached. File types suchas 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 different. It refers to the pseudo fileformed 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 beenused will cause @ARGV to be examined to determine if input isavailable. Similarly, an eof() after <> has returnedend-of-file will assume you are processing another @ARGV list,and if you haven't set @ARGV, will read input from STDIN;see I/O Operators in perlop.

    In a while (<>) loop, eof or eof(ARGV) can be used todetect the end of each file, whereas eof() will detect the end of the very last file only. Examples:

    1. # reset line numbering on each input file
    2. while (<>) {
    3. next if /^\s*#/; # skip comments
    4. print "$.\t$_";
    5. } continue {
    6. close ARGV if eof; # Not eof()!
    7. }
    8. # insert dashes just before last line of last file
    9. while (<>) {
    10. if (eof()) { # check for end of last file
    11. print "--------------\n";
    12. }
    13. print;
    14. last if eof(); # needed if we're reading from a terminal
    15. }

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

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Raise an exception or bail outReturn file descriptor from fi ... (Berikutnya)