Given a root directory I wish to identify the most shallow parent directory of any .svn directory and pom.xml .
为实现这一目标,我确定了以下职能:
use File::Find;
sub firstDirWithFileUnder {
$needle=@_[0];
my $result = 0;
sub wanted {
print " wanted->result is $result
";
my $dir = "${File::Find::dir}";
if ($_ eq $needle and ((not $result) or length($dir) < length($result))) {
$result=$dir;
print "Setting result: $result
";
}
}
find(&wanted, @_[1]);
print "Result: $result
";
return $result;
}
因此:
$svnDir = firstDirWithFileUnder(".svn",$projPath);
print " Identified svn dir:
$svnDir
";
$pomDir = firstDirWithFileUnder("pom.xml",$projPath);
print " Identified pom.xml dir:
$pomDir
";
我无法解释两种情况:
- When the search for a .svn is successful, the value of
$result
perceived inside the nested subroutinewanted
persists into the next call offirstDirWithFileUnder
. So when the pom search begins, although the linemy $result = 0;
still exists, thewanted
subroutine sees its value as the return value from the lastfirstDirWithFileUnder
call. - If the
my $result = 0;
line is commented out, then the function still executes properly. This means a) outer scope (firstDirWithFileUnder
) can still see the$result
variable to be able to return it, and b) print shows thatwanted
still sees$result
value from last time, i.e. it seems to have formed a closure that s persisted beyond the first call offirstDirWithFileUnder
.
有些人是否可以解释正在发生什么,并建议我如何在进入外层空间时适当重新确定<条码>(按美元/<>/代码”的价值?