As you point out, iterating over a file using foreach interrupts the file in memory before the loop starts.
Although it is true that one of Perl's crosses is βThere is more than one way to do this,β there are ways to do things that have no real benefit compared to any other way. foreach is probably a bad alternative for looping around a file. If you are going to iterate over a file line by line, you usually use while() . If you are going to decompose the entire file into memory at once, you do it in one step and execute it.
Sometimes it is useful to rip the whole file. For example, one of them minimizes file open time. Most often, this is convenient for working with data that does not really correspond to a string record or recording by a recording model. foreach () deletes the file, but actually does not give a good opportunity to close it immediately, so the good will disappear. And looping through a file using foreach will not help you if your data should be considered as a piece. So the reason is gone too. In the end, you get a negative file breakaway, without any positive effects for file corruption.
There will always be someone who comes up with a smart reason that others have not considered. But I have not seen it yet. (Usually, when I say something like this, someone writes βthis,β so itβs always dangerous to assume that he has absolutely no merit.) Let it be so: until you find one of the very few For good reasons using foreach to iterate over a file, don't worry about it. Of course, when you make this discovery, you will realize that the time has come.
source share