By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

Perl EOF keyword?

P: 74
I was wondering if there was an end of file keyword or whatever like C/C++ has to determine if you've reached the end of a file. I want to read in a file and display it line by line. When the file has reached the end, I want to print a statement to the user telling them that the previous line was the end of the file. How can I do this?
Apr 4 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
perl has the eof function:

Expand|Select|Wrap|Line Numbers
  1. open(FH, 'filename');
  2. while(<FH>){
  3.    print if eof;
  4. }
  5. close FH;
Apr 4 '08 #2

P: 74
perl has the eof function:

Expand|Select|Wrap|Line Numbers
  1. open(FH, 'filename');
  2. while(<FH>){
  3.    print if eof;
  4. }
  5. close FH;
Can you tell me what exactly print if eof; does?
Apr 4 '08 #3

P: 74
I have to print a file forwards or backwards depending on the radio button the user selects. Can you tell me if I have the right idea?

Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. if($FORM{'forward'})
  2. {
  3.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  4.         $i = 0;
  5.         $line = <$FORM{'filename'}>;
  6.         while(<file>)  
  7.         {
  8.                 if(!eof())
  9.                 {
  10.                         print "Line: $i $line[i]";
  11.                         $i++;
  12.                 }
  13.                 else
  14.                 {
  15.                         print "End of file";
  16.                 }
  17.         }
  18. #       my $line;
  19. #       while($line = <file>)
  20. #       {
  21. #               print "$line\n<br>";
  22. #       }
  23.         close(file);
  24. }
  25. elsif($FORM{'reverse'})
  26. {
  27.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  28.         $i = 0;  
  29.         $line = <$FORM{'filename'}>;
  30.         while(<file>)
  31.         {
  32.                 if(!eof())
  33.                 {
  34.                         print "Line: $i $line[i]";
  35.                         print reverse map{chomp; reverse($_) . " <br>\n"} <file>;
  36.                         $i++;
  37.                 }
  38.                 else
  39.                 {
  40.                         print "End of file";
  41.                 }
  42.         }
  43.  
  44. #       my $line;
  45.         close(file);
  46. }
  47.  
Apr 4 '08 #4

eWish
Expert 100+
P: 971
Can you tell me what exactly print if eof; does?
EOF - Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open.

--Kevin
Apr 4 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Can you tell me what exactly print if eof; does?
it prints the last line of the file.
Apr 5 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
I have to print a file forwards or backwards depending on the radio button the user selects. Can you tell me if I have the right idea?

Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. if($FORM{'forward'})
  2. {
  3.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  4.         $i = 0;
  5.         $line = <$FORM{'filename'}>;
  6.         while(<file>)  
  7.         {
  8.                 if(!eof())
  9.                 {
  10.                         print "Line: $i $line[i]";
  11.                         $i++;
  12.                 }
  13.                 else
  14.                 {
  15.                         print "End of file";
  16.                 }
  17.         }
  18. #       my $line;
  19. #       while($line = <file>)
  20. #       {
  21. #               print "$line\n<br>";
  22. #       }
  23.         close(file);
  24. }
  25. elsif($FORM{'reverse'})
  26. {
  27.         open(file, "<$FORM{'filename'}") || die "Cannot open file: $!\n";
  28.         $i = 0;  
  29.         $line = <$FORM{'filename'}>;
  30.         while(<file>)
  31.         {
  32.                 if(!eof())
  33.                 {
  34.                         print "Line: $i $line[i]";
  35.                         print reverse map{chomp; reverse($_) . " <br>\n"} <file>;
  36.                         $i++;
  37.                 }
  38.                 else
  39.                 {
  40.                         print "End of file";
  41.                 }
  42.         }
  43.  
  44. #       my $line;
  45.         close(file);
  46. }
  47.  
Does the code work? Do you mean print the lines in reverse order or print each line in reverse order (or both)? For exmple:

file:

foo bar
bar baz

reverse order:

bar baz
foo bar

each line reversed but in the same order:

rab oof
zab rab

each line reveresed in reverse order:

zab rab
rab oof


I don't think your attempt to print the file in reverse order will work like you want. The print command is a list operator, so when you do this:

print <filehandle>;

it prints all the lines in the file associated with the filehandle. There is no way to check for eof because you just printed all the lines.
Apr 5 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.