in reply to Adding a dispatch table and getting "Variable $x will not stay shared" errors.
Also, there's no point in having named nested subroutines. Instead of \&wanted, just use an anonymous subroutine...but File::Find doesn't work like what you are trying to do anyway. you shouldn't try to return things from your wanted function (find() doesn't return anything useful), you might set a variable in an outer scope though.
But even with this, do you really want to search all subdirectories of /tmp/dir1 with find()? Or is -f "$location/target" what you want? Is "$target" supposed to be a regular expression? Or just the file basename (in which case you would want "if $_ eq $target" in the function)?sub findfile { my $target = $_[0]; my $location = "/tmp/dir1"; my $foundfile; $target = qr/$target/; my $filename = find(sub { ($foundfile = $File::Find::name) if $File::Find::name =~ $targ +et; }, $location); print $filename; }
Update: Oops. I removed the accidentally left in "return $foundfile".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding a dispatch table and getting "Variable $x will not stay shared" errors.
by gctaylor1 (Hermit) on Mar 03, 2009 at 02:02 UTC | |
by runrig (Abbot) on Mar 03, 2009 at 16:30 UTC |