The first example works because the lexical @files is visible to the FileName subroutine (since both are defined within the Get_Saves subroutine block). This works because you have the scope correct.
The second example will not work for several reasons: 1, File::Find::find does not return anything; 2, \&FileName(\@files) is not a reference to a subroutine (only \&FileName is); and three, the @files in Get_Saves is a different beastie than the @files in FileName (the first is a lexical and the second is a global.
To use File::Find with the two seperate functions, you would need to use either global @files or a lexical @files defined outside of Get_Saves and FileName:
#!/usr/bin/perl use strict; use File::Find; my @files; # could leave this out and just let FileName # auto-vificate global @files Get_Saves(); sub Get_Saves { find( \&FileName, "/usr/local/apache/htdocs/service/" ); } sub FileName { unless( ($_ eq ".") or ($_ eq "temp.svd")) push( @files, $_ ); # This @files is either the lexical declared above # or an auto-vifified global if nothing was declared # above. }
update I really like goldclaw's Re: Sub in a Sub & Recursion but you would need to remove the "@files=" from the find since File::Find::find always returns nothing. Just do this:
#where to add stuff. find(FileName(\@files),"/usr/local/apache/htdocs/service/"); return \@files, $#files+1;
-derby
In reply to Re: Sub in a Sub & Recursion
by derby
in thread Sub in a Sub & Recursion
by hakkr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |