The function eof will return true after you've read all of the lines of the file. A simple example:

open FH, '<', 'file.txt' or die ("Can't read file: $!"); until( eof(FH) ) { print <FH>; }

However, you rarely need to use eof() to accomplish what you are asking about. Imagine that you want read in a text file and, when finished, print the average length of lines (excluding any ending whitespace) to the screen, along with the number of lines. You might do that this way:

open FH, '<', 'file.txt' or die ("Can't read file: $!"); my ($lines, $total_length); while (<FH>) { s/\s+$//s; # trims all traling whitespace, including \n ++$lines; $total_length += length($_); } #loop ends automatically when you reach EOF printf "File had %d lines, with an average length of %d chars\n", $lines, ($total_length / $lines);

You see? You don't need to check for EOF by hand, as Perl does it for you.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: Example for EOF by radiantmatrix
in thread Example for EOF by l.frankline

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.