This is one of the Common Beginner Mistakes
. (No offense, any experienced programmer who claims not to have asked the same question once is either Donald Knuth or a liar.)
If you have a list of items to process and you don't know how long the list will be at compile-time, use a stack. In Perl, that's as simple as using push to add another element to an array. That's exactly what arturo suggests.
Attempting to keep track of a bunch of names generated on the fly can be tricky, and in this case, it's probably completely unnecessary.