providencia has asked for the wisdom of the Perl Monks concerning the following question:

I have this code:

#opening the logfile to check for logout
open (CHECKLOGOUT, "/$username/perl/lrnprl.log") || die "\nCouldn't open file to check for logout status\n";

#the code that checks the "end of file" (eof) for the character "#"
#and "if" the file ends with "#" 
#it....
#1) clears the screen
#2) prints the exit message
#3) closes the filehandle
#4) exits the program.
if (eof("#")){
system ("cls");
print "\n\n\n\n\n\n\t\tEXITING PROGRAM IMMEDIATELY\n\n\n\t\t !!YOU MUST FIRST LOGOUT!!\n\n\n\n\n\n";
close (CHECKLOGOUT);
exit 0;
}
This will exit the program no matter what!
I think I can do this with eof(). A friend showed me some code using seek() and read(). 
I want to use eof() because I'm stubborn.
Any help is appreciated.
  • Comment on Reading the last character of a file and/or line.

Replies are listed 'Best First'.
Re: Reading the last character of a file and/or line.
by btrott (Parson) on Apr 07, 2000 at 04:51 UTC
    That's not how eof works. Did you read the docs? It takes a filehandle as an argument and returns TRUE if the next read on the filehandle will return end of file.

    It does *not* take a character as an argument and search for that character.

    Your friend was right--you're going to have to use seek and read. Try

    seek FH, -1, 2; read FH, $char, 1; if ($char eq "#") { ...
    Of course, if the last character of your file is actually a carriage return, this won't work; in that case, you'll need
    seek FH, -2, 2;
Re: Reading the last character of a file and/or line.
by providencia (Pilgrim) on Apr 07, 2000 at 06:57 UTC
    Yes. I was spinning my wheels. I will use seek and read for what I need. I remember reading that in the perldoc about eof(). I don't know why I got myself stuck there. Thanks for a speedy response. I'm working on a perl logger that keeps track of how much time I spend learning perl. :)

    A project that involves practice, learning and constant re-development. :)
    I'll post it when I feel good about it.