Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check EOF read error

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
I know there is the built-in eof function, but that seems to tell you that the NEXT read will be EOF. Is there a way to read a line and check if a returned error is EOF?

For example:
Code:
$line = <THIS> || goto DONE if ($the_error == EOF);
Where $the_error is like $? or something like that...and EOF is some built-in error number??

Thanks!
 
Code:
goto DONE if (eof);

are you really using "goto"?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
perlfunc eof said:
Practical hint: you almost never need to use eof in Perl, because the input operators typically return undef when they run out of data, or if there was an error.
The customary way of doing that in perl is
Code:
while($line=<THIS>){
  ...
}
DONE: ...

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Believe me, I HATE "goto"...but, trust me, I have my reasons in this case!?!?!?

Let me expand my example. Again, the "eof" tells me that the NEXT read will be EOF, correct? If that is trully the case, this next example will NOT print all the lines of the file...it will skip the last line. FYI: I don't have much leeway on re-structuring this...because, obviously, there's a MUCH better way of doing this!!!
Code:
AGAIN:
  $line = <THIS> || goto DONE if ($the_error == EOF);
  print $line;
  goto AGAIN;
DONE:
 
...because, obviously, there's a MUCH better way of doing this!!!"

...which prex1 descibed!

I'm really just looking to see if there is an error I can trap to tell me if the error was EOF or some other read error.

Thanks.
 
just use a "while" loop and loop controls like "next", "last", "redo", as necessary.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I hate to sound like a broken record...but, I don't have any control over the structure being used (i.e., can't change it to a while loop). Therefore, I'm only looking to see if there is a way to distiguish a read error for an EOF vs. any other error?

Thanks.
 
Have you tried this?

Code:
AGAIN:
  $line = <THIS> or goto DONE;
  print $line;
  goto AGAIN;
DONE:

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

Thanks for the response. Yes, that works, but that will "goto DONE" on ANY error. I just need it to "goto DONE" on EOF.
 
Dude - you're in panic mode. The answer is above
Code:
AGAIN:
  $line = <THIS> ;
  goto DONE if (eof) ;
  print $line;
  goto AGAIN;
DONE:

This isn't exactly my business - but what would preempt you from using a while loop?
 
PinkyNBrain,

My understanding is that the "eof" function tells you that the NEXT line read will be EOF. Therefore, in your example, the last line will not be printed.

FYI: I'm writing a code converter and complexities in the code being converted will not allow me to "re-arrange" things.
 
This is fairly old school style of reading from files. This was (is?) the norm for older languages like pascal and PL1. Consider the following
Code:
$line = <DATA> ;
goto DONE if (eof);
AGAIN:
   print "in loop $line";
   goto DONE if (eof);
   $line = <DATA> ;
   goto AGAIN ;
DONE:
print "end of loop $line\n";

__DATA__
not last line
yes last line

The answer was there....
 
So, I'm getting the impression that read does not return any error code that I could correspond with EOF (like my first postings "psuedo-code")? If it did, all my problems would go away :)
 
By the way...thanks for the "Good Luck". I'll definitely need it!!!!
 
In case you think your data may contain only 1 (one) line of data
Code:
$line = <DATA> ;
goto ONE_LINE if (eof);
goto AGAIN ;
ONE_LINE:
print "in loop $line";  # sort of 'in loop'
goto DONE ;
AGAIN:
   print "in loop $line";
   goto DONE if (eof);
   $line = <DATA> ;
   goto AGAIN ;
DONE:
print "end of loop $line\n";


__DATA__
not last line
yes last line
If you remove one of the entries from under __DATA__, this one will still work.
 
The more I looked at that last post, the worse it looked. One last itteration
Code:
$line = <DATA> ;
AGAIN:
   print "in loop $line" if ($line =~ /./);
   (eof) ? (goto DONE) : ($line = <DATA>) ;
   goto AGAIN ;
DONE:
print "end of loop $line\n";


__DATA__
not last line
yes last line
 
OOOO, I think that's getting REALLY close to what I want. Let me think about that a bit....

THANKS!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top