Increase the scope of @dirlist:
use File::Find; my @dirlist; sub wanted { return unless -d $_; push(@dirlist, $File::Find::name); } find(\&wanted, shift(@ARGV)); # Use @dirlist here
Closures come in useful if you'd wish to avoid using a global:
use File::Find; sub find_dirs { my @dirlist; find(sub { return unless -d $_; push(@dirlist, $File::Find::name); }, @_); return @dirlist; } { # Not a global. my @dirlist = find_dirs(shift(@ARGV)); # Use @dirlist here }
Yet another way. This one uses dynamically scoped variables:
use File::Find; sub wanted { return unless -d $_; push(@File::Find::matches, $File::Find::name); } sub find_it { local *File::Find::matches; find(@_); return @File::Find::matches; } { # Not a global. my @dirlist = find_it(\&wanted, shift(@ARGV)); # Use @dirlist here }
Update: Added 2nd snippet.
Update: Added 3rd snippet.
In reply to Re: Return values, Closure Vars from File::Find &wanted subs?
by ikegami
in thread Return values, Closure Vars from File::Find &wanted subs?
by Subliminal Kid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |