CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
about End of File

 



kevindai
Novice

Apr 11, 2009, 8:18 AM

Post #1 of 7 (653 views)
about End of File Can't Post

I am trying to find some particular word in a file. Is there such keyword like EOF in Perl?
How do i do that? I am trying something like:

open (FILE,"list.txt")||die"can't open symbol.txt :$!\n";
my $store = <FILE>;

while($store != EOF)
{
search......
}


FishMonger
Veteran / Moderator

Apr 11, 2009, 8:52 AM

Post #2 of 7 (652 views)
Re: [kevindai] about End of File [In reply to] Can't Post

eof - test a filehandle for its end
http://www.perl.com/doc/manual/html/pod/perlfunc/eof.html

The normal/preferred idiom is:

Code
my $fname = 'list.txt'; 
open my $file, '<', $fname or die "can't open '$fname' $!";

while( my $store = <$file> ) {
# search/process $store
}



Beastie
Novice

Apr 11, 2009, 9:51 AM

Post #3 of 7 (649 views)
Re: [FishMonger] about End of File [In reply to] Can't Post

Woah, can you use variables as filehandles? (<$file>)
The reason I ask is I feel like I've tried to do that before and it didn't work. I could certainly be wrong, since I wrote the script I tried that in a while ago and I don't completely remember it.


FishMonger
Veteran / Moderator

Apr 11, 2009, 10:34 AM

Post #4 of 7 (646 views)
Re: [Beastie] about End of File [In reply to] Can't Post

Using a lexical var for a filehandle is the preferred method instead of a bareword.


Beastie
Novice

Apr 11, 2009, 2:09 PM

Post #5 of 7 (640 views)
Re: [FishMonger] about End of File [In reply to] Can't Post

That's cool.


kevindai
Novice

Apr 11, 2009, 2:19 PM

Post #6 of 7 (637 views)
Re: [FishMonger] about End of File [In reply to] Can't Post

That helped a lot! Thank you!


KevinR
Veteran


Apr 11, 2009, 4:13 PM

Post #7 of 7 (632 views)
Re: [Beastie] about End of File [In reply to] Can't Post


In Reply To
Woah, can you use variables as filehandles? (<$file>)
The reason I ask is I feel like I've tried to do that before and it didn't work. I could certainly be wrong, since I wrote the script I tried that in a while ago and I don't completely remember it.


Here is some reading from perldoc concerning indirect (or lexical) filehandles:

http://perldoc.perl.org/perlopentut.html#Indirect-Filehandles
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives