Thread File::Find Warnings (11 answers)
Opened by bianca at 2013-04-04 21:30

pq
 2013-04-04 21:40
#166889 #166889
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
use diagnostics

Quote
Variable "%work" will not stay shared at test.pl line 18 (#1)
(W closure) An inner (nested) named subroutine is referencing a
lexical variable defined in an outer named subroutine.

When the inner subroutine is called, it will see the value of
the outer subroutine's variable as it was before and during the *first*
call to the outer subroutine; in this case, after the first call to the
outer subroutine is complete, the inner and outer subroutines will no
longer share a common value for the variable. In other words, the
variable will no longer be shared.

This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs that
reference variables in outer subroutines are created, they
are automatically rebound to the current values of such variables.


Code (perl): (dl )
1
2
3
4
5
6
    my $file_find_wanted = sub {
        if (-f $File::Find::name) {
            $work{gefunden} ++;
        }
    }
    find($file_find_wanted, ('/www'));
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread File::Find Warnings