in reply to Conditional creation of files - global or scoped filehandle
Should the filename be declared outside of the loop first?
Yes, that's the obvious approach IMO. (you did mean filehandle there of course)