in reply to Re^2: foreach vs while<>
in thread foreach vs while<>
If you want a line at a time, while (<$fh>) is perfect.
An array would be useful if you need to go through the file multiple times. Or if you need to traverse the file in an odd order. Or if you want to sort the contents. Sometimes, I'll load the entire file into memory so I can use grep or map, although I tend not to use an array in between.
And then there times when you want to load the entire file into a string, so you'd use local $/; $text = <$fh>;.
And then there are files which don't have a concept of lines, so you'd use read or sysread.
One this is for certain is that there is no reason to do @a = <$fh>; for (@a) unless you do something else with @a. That's the same thing as doing for (<$fh>), and there's no reason for that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: foreach vs while<>
by JavaFan (Canon) on Nov 12, 2008 at 12:21 UTC | |
by ikegami (Patriarch) on Nov 12, 2008 at 17:31 UTC | |
|