Can I tell if *DATA is exhausted? (eof, etc?)

U

usenet

According to perldoc -f eof:

eof Returns 1 if the next read on FILEHANDLE will return end of file

This does not seem to be true when reading __DATA__. The function
returns 1 no matter what. For example:

#!/usr/bin/perl
for (<DATA>) {
print eof;
}
__DATA__
1
2
3

will print "1" three times.

Is there a way to determine if *DATA has been exhausted?
 
A

Anno Siegel

According to perldoc -f eof:

eof Returns 1 if the next read on FILEHANDLE will return end of file

This does not seem to be true when reading __DATA__. The function
returns 1 no matter what. For example:

#!/usr/bin/perl
for (<DATA>) {
print eof;
}
__DATA__
1
2
3

will print "1" three times.

Not for me (v5.8.7 built for i586-linux). I see two false values and
one true one.

Anno
 
U

usenet

Anno said:
Not for me (v5.8.7 built for i586-linux). I see two false values and
one true one.

Really??? I'm not far behind you (perl, v5.8.6 built for
i586-linux-thread-multi) but I get three true's!

I wonder if this is a bug that was fixed in 5.8.7???
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
According to perldoc -f eof:

eof Returns 1 if the next read on FILEHANDLE will return end of file

This does not seem to be true when reading __DATA__. The function
returns 1 no matter what. For example:

#!/usr/bin/perl
for (<DATA>) {
print eof;
}
__DATA__
1
2
3

will print "1" three times.

Is there a way to determine if *DATA has been exhausted?

Well, using the for loop causes *DATA to be slurped. By the time the
loop has started, *DATA has already been exhausted, so what you is not
surprising.

If you use a while loop, read line-by-line, you should observe only one
1 being printed.

Sinan
 
U

usenet

A. Sinan Unur said:
If you use a while loop, read line-by-line, you should observe only one
1 being printed.

True (I see '1" - one 1). But I still don't understand why I don't see
'001' (as Anno reports with "for"). I don't understand why I see only
"1", because if I say:

#!/usr/bin/perl
while (<DATA>) {
print $_;
}
__DATA__
1
2
3

I see "123". So if I "print eof" instead of "print $_" then why do I
only see "1"? I seem to be very confused.
 
A

Anno Siegel

Really??? I'm not far behind you (perl, v5.8.6 built for
i586-linux-thread-multi) but I get three true's!

I wonder if this is a bug that was fixed in 5.8.7???

Sinan has recognized the problem, it's the for-loop.

While-loops for input, for-loops for everything else.

Anno
 
U

usenet

Anno said:
Sinan has recognized the problem, it's the for-loop.

While-loops for input, for-loops for everything else.

OH, Duh! Sinan may disregard my question, which piles ignorance upon
ignorance.

Thanks both!
 
A

Anno Siegel

True (I see '1" - one 1). But I still don't understand why I don't see
'001' (as Anno reports with "for"). I don't understand why I see only
"1", because if I say:

I reported "two false and one true values", not "001".
#!/usr/bin/perl
while (<DATA>) {
print $_;
}
__DATA__
1
2
3

I see "123". So if I "print eof" instead of "print $_" then why do I
only see "1"? I seem to be very confused.

Perl's boolean "false" has a numeric value of 0, but a string value of
"". If you print it, you see the string, i.e. nothing. Print with
line feeds.

Anno
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
OH, Duh! Sinan may disregard my question, which piles ignorance upon
ignorance.

Don't worry about it. Happens to everyone. I stared at your post for a
good five minutes before I realized you were using a for loop. The brain
sees what it wants to see.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top