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.