use File::Find; # set up closure here my @dirlist; sub wanted { return unless -d $_; push(@dirlist,$File::Find::name); } # call the sub to fill @dirlist find(\&wanted, shift(@ARGV)); # use results here print "$_\n" for @dirlist;
@dirlist is visible within the sub, and the push will work as normal. A "closure" essentially boils down to using a lexical ("my") variable from outside a subroutine inside a subroutine. See the Closure on Closures tutorial.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
In reply to Re: Return values, Closure Vars from File::Find &wanted subs?
by xdg
in thread Return values, Closure Vars from File::Find &wanted subs?
by Subliminal Kid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |