l.frankline has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everybody,

I have a small problem, let me explain....

I use a text file, it contains for approx. 200 lines, i want to display a message when i reach end of the file, i tried eof() function..but i couldn't..

Please give an example for eof()

Thank you all

Regards
Franklin

Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re: Example for EOF
by duff (Parson) on Nov 15, 2005 at 05:49 UTC

    Normally you don't use eof but rather check the return value of whatever you're using to read the filehandle. Usually that's the diamond operator (<>, aka readline):

    open my $filehandle, "<", $filename or die "Oof! $!"; while (<$filehandle>) { do_something(); } close $filehandle; do_something_else()

    When the conditional of the while becomes false, then there are no more lines to read and you've reached the end of the file and in your case do_something_else looks like it should display a message.

Re: Example for EOF
by spiritway (Vicar) on Nov 15, 2005 at 05:46 UTC

    I'm not sure what the problem is, exactly. Do you want to just know when the program has finished reading?

    use strict; use warnings; my $file = "file.txt"; open FH, "<", $file; while(<FH> { # do something here... } print "End of file has been reached\n";

    I think that ought to do it for you, though I haven't actually tested the code.

Re: Example for EOF
by Aristotle (Chancellor) on Nov 15, 2005 at 05:48 UTC

    The majority of the time, using eof is a mistake.

    What code have you written so far? If we had an example, someone could show you how to achieve what you want. (Please keep this in mind: it’s never a good idea to ask about a particular programming problem without posting any code.)

    Makeshifts last the longest.

Re: Example for EOF
by jZed (Prior) on Nov 15, 2005 at 05:50 UTC
    You don't need an explicit function to mark the end of a file. The following will loop through the lines of the file. When the end of the file is reached, you'll exit the loop so you can display your end-of-file message immediately after the loop.
    #!/usr/local/bin/perl use warnings; use strict; my $filename = 'your_file'; open my $filehandle, '<', $filename or die $!; while (my $line = <$filehandle>) { print $line; } print "End of File!\n";
Re: Example for EOF
by l.frankline (Hermit) on Nov 15, 2005 at 06:29 UTC

    Hi guys,

    Thank you those who have responded for my question..

    i am sorry to say that, i didnt get what i expected..

    my aim is: when i reach the last line of the file.. i will add some text to that line and finish the program.

    pls help me..

    Thanks

    Franklin

    Don't put off till tomorrow, what you can do today.

      I beleive then what you want to do is to open in "+>" mode for read write. Then read the file then start writing.

      If all you're doing is writing to the end of the file, it's easy. When you open it, open it to "append" - meaning, what you write is added to the end of the file.

      open FH, ">>", $file or die "Can't open $file: $!";

      It's always a good idea to test when you open, in case something happened. If you don't, you may have a very hard time figuring out what went wrong, later on.

      So, it sounds like you are saying "I want to append data to the end of my file." If that's true, follow the advice of the comment below and read up on the open() function. It tells you how to open a file to append it.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

      my aim is: when i reach the last line of the file.. i will add some text to that line and finish the program.

      Oh "XY", then: you really want to ask X, but you think you need Y to do that, and ask about Y.

      This sounds like: "I want to append to the end of file". If so, then just either open in >> mode if you only want to append, or +> if you also want to read it before writing something in it.

Re: Example for EOF
by kulls (Hermit) on Nov 15, 2005 at 09:32 UTC
    Hi,
    we will do this using seek() function.
     seek($FILEHANDLE,2,SEEK_END).
    This will set the file handler to eof position.So we can able to start writing from the end.
    more info:
    http://perldoc.perl.org/functions/seek.html Regards kulasekar.
Re: Example for EOF
by radiantmatrix (Parson) on Nov 15, 2005 at 16:13 UTC

    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