1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/perl -w use strict; use warnings; use File::Find; test(); sub test { my %work = ( gefunden => 0, ); { no warnings 'File::Find'; find(\&file_find_wanted,('/www')); } sub file_find_wanted { if (-f $File::Find::name) { $work{gefunden} ++; } } print $work{gefunden}; }
QuoteVariable "%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.
1 2 3 4 5 6
my $file_find_wanted = sub { if (-f $File::Find::name) { $work{gefunden} ++; } } find($file_find_wanted, ('/www'));
2013-04-05T07:07:17 MuffiBenutzt man eigentlich inner-subs?
1 2 3 4
use File::Find::Rule; my %work; $work{gefunden} = scalar File::Find::Rule->file()->in('/www');
2013-04-05T08:24:05 MuffiCode (perl): (dl )$work{gefunden} = scalar File::Find::Rule->file()->in('/www');
$i += unlink($_) for File::Find::Rule->file->empty->in('/www');
2013-04-05T09:58:12 pqes ist aber quatsch.