while (@a) { $dirtocheck= shift @a; ($dirs,$files)= vendorprogram($dirtocheck); unshift @a,@$dirs; push @result,$dirtocheck,@$files }