in reply to How to open file inside loops with file names generated using an array?

It is often a good idea to use something like File::Find (there are many flavors of this) to first locate the files you want to change, putting the names into an array that you then loop through. Opening and manipulating files within a loop that is also finding them can cause problems in some environments.
  • Comment on Re: How to open file inside loops with file names generated using an array?

Replies are listed 'Best First'.
Re^2: How to open file inside loops with file names generated using an array?
by Anonymous Monk on May 28, 2018 at 16:36 UTC
    Opening and manipulating files within a loop that is also finding them can cause problems in some environments.

    wat? examples please

      It depends on the operating system, but a file-finder can skip file entries or see the same entry more than once ... a problem that is neatly avoided by completely scanning, say, a particular directory, then processing that list before moving on to the next one. (I have not looked closely at Perl's implementation of file-finders, but in other languages I noticed them doing this internally, and wondered why they did it that way.) Particularly if you are inserting, deleting, or renaming entries in the designated container, this approach can avoid a lot of messy problems.
        If you are merely reading or updating the files, it usually doesn't matter. Troubles start when you are changing the list-of-files or file-names underneath the walker's feet while it's walking.