Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Trying to do multiple while loops on the same input file

by zek152 (Pilgrim)
on Jun 01, 2011 at 14:28 UTC ( [id://907641]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Trying to do multiple while loops on the same input file
in thread Trying to do multiple while loops on the same input file

It is much less a Perl way of doing things and more of an OS way of doing things. Perl gives you the seek function to do exactly what you want. Operating systems are made to handle 1kB files and also handle 3gB files. For your particular problem everything can be stored in ram (and possibly in a cache). For other problems the whole file cannot be stored in RAM.

I am sorry that you spent hours looking for the issue. Trust me when I say that learning how file reading works will help you no matter what language you are using. I personally do not know of a language that provides the functionality that you desire in the basic read(file).I can tell you that C, C++, C#, Java, Perl and Python all read files in a start to finish manner.

Update: fixed small typo.

Replies are listed 'Best First'.
Re^4: Trying to do multiple while loops on the same input file
by elef (Friar) on Jun 01, 2011 at 15:07 UTC
    I can tell you that C, C++, C#, Java, Perl and Python all read files in a start to finish manner.

    I guess you meant "once only" instead of start to finish. That's fair enough, but if a command is going to fail, perl might as well give me some information about it. When you try to read a filehandle that never even existed, you get an "unopened filehandle" error message. I don't see why you shouldn't get an error message about an "already read" or "expired", "inactive" filehandle or something to that effect instead of the command failing silently, which makes debugging a nightmare.

      "I guess you meant 'once only' instead of start to finish."

      I did not mean that. Perhaps a better way would have been for me to say a beginning of file to end of file manner. All the aforementioned languages support some sort of seek which allows you to change the current position in the file.

      The reason that you should not get an error message is because the while(<FILE>){...} is behaving exactly as documented and in my opinion it behaves intuitively.

      As stated in perl documentation <FILE_HANDLE> will return undef when there is nothing left to read at the current location in the file (aka the end of the file). Undef evaluates as false and therefore the body of the while loop is never executed. It is important to note that the command did not fail, silently or otherwise. The command behaved exactly as documented. The filehandle is not "already read","expired" or "inactive". There is simply nothing left to be read at the current position in the file.

      I believe the primary issue is that your understanding of how file reading should work does not match how file reading actually works on modern operating systems. At the moment I do not have any advice on places to learn more about file reading other than the perldoc.perl.org. Perhaps another PerlMonk would have some additional reference material that would help you better understand files, file descriptors, and file reading.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://907641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found