似乎它清理垫太早了:
sub search {
my ( $self, $test ) = @_;
my $where;
my $found = 0;
my $counter = 0;
$self->descend( pre_each => sub {
my $lvl = shift;
my $ev_return
= $lvl->each_value( sub {
$counter++;
my ( $name, $value ) = @_;
say "\$name=$name";
say "\$value=$value";
return 1 unless $found = $test->( $value );
$where = { key => $lvl, name => $name, value => $value };
# when any intermediate function sees QUIT_FLAG, it
# knows to return control to the method that called it.
return QUIT_FLAG;
});
say "\$found=$found";
say "\$where=$where";
return $ev_return;
});
say "\$counter=$counter";
say "\$found=$found";
say "\$where=$where";
return unless $found;
return $where;
}
我得到的是:
...
$found=1
$where=HASH(...)
$counter=0
$found=0
$where=
或者,如果有人能指出我正在做的蠢事,我将不胜感激。我什至在第一个闭包和外部闭包之间创建了增量变量,但它们也被重置了。即使在最里面的闭包上设置引用,在命名的子范围内也得不到任何东西!
这里涉及的整个代码是 500 行。包含代码是不切实际的。