http://qs321.pair.com?node_id=1031085

vagabonding electron has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

this is most certainly a misunderstanding of a scope issue but I cannot solve this puzzle myself.

The following code works:

#!/perl use strict; use warnings; use File::Find; my $dir = qw(N:/TMP); find( \&wanted, $dir ); sub wanted { print "[$dir] $File::Find::dir $File::Find::name\n"; }

However if I try to call the same in the loop:

my @dir = ( qw( N:/TMP N:/TMP_2 )); for my $dir ( @dir) { find( \&wanted, $dir ); }

it dies with the message:

Global symbol "$dir" requires explicit package name at ...

If I call the same as an anonyme subroutine, it works:

for my $dir ( @dir) { find( sub {print "[$dir] $File::Find::dir $File::Find::name\n";}, $dir ); }

Could you please explain this behavior?

Thanks in advance.

VE