It seems like you are trying to build up a deeply nested data structure which looks a lot like your directory tree. However, if all you need is a simple array of full paths to files, take a look at File::Find::Rule, which recursively traverses the filesystem.