Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!!

I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively.

Now the problem is that it works well if i use it with one time but the problem is that when i try to use this function inside a for loop passing each iteration value as argument to this function this don't work and i loose the results of every iteration . I tried to write teh o/p to a file before next iteration but it is not writing anything inside it .

Here is the code -

#!/usr/bin/perl

use warnings;

our @all_dirs;

sub list_dirs {

my $dir = shift;
#print "$dir\n";
opendir DIR, $dir or return;
 my @contents = map "$dir/$_", sort grep !/^\.\.?$/, readdir DIR;
closedir DIR;
 #print @contents;
 #sleep(20);
  
 
 foreach (@contents) 
     
 {
next unless !-l && -d;
 &list_dirs($_);

 push(@all_dirs,"$_\n");

 
 }
return @all_dirs;
 

} 

opendir MYDIR, "C:\\temp";

@all = grep !/^\.\.?$/, readdir MYDIR;



 foreach $i (@all) 
     {

@var=&list_dirs($i);
print @var;
open F1,">>C:\\$i._subdirs.txt";
foreach $e (@var) { print "$e\n";print F1 "$e\n";}
close F1;

#

}