Perl 5.12.3 的引用计数问题?
看来它清理垫子太早了:
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 行。包含代码是不切实际的。
It seems that it's cleaning up the pad too early:
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;
}
And what I get is:
...
$found=1
$where=HASH(...)
$counter=0
$found=0
$where=
Or, if anybody can point to something bone-headed I'm doing, I'd really appreciate it. I even created incremental variables between the first and outer closure, but they got reset too. Even setting references on the innermost closure, gets me nothing in the named sub scope!
The entire code concerned here is 500 lines. It is impractical to include the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您能提供一个完整的、可运行的示例,那就太好了。
在黑暗中刺伤:在外部匿名子中额外使用 $found 是否有帮助(例如
$found if 0;
)?It would be really good if you could provide a complete, runnable example.
Stab in the dark: does it help to have an extraneous use of $found in the outer anonymous sub (e.g.
$found if 0;
)?不要将
my
与语句修饰符一起使用!结果发现问题出在一个被调用的范围内。由于忘记了针对使用
my
与语句修饰符的警告,我编写了以下代码:第一次通过
@_
有一个参数。它将第一个值分配给$each
。第二次通过时,由于参数较多,它跳过了my
。因此当前作用域中没有声明,因此它只是重用了我上次分配的 sub,并且在%params
中没有保存任何内容,因为它引用了$each
有一个值。很奇怪,但正如 ysth 指出的 perlsyn 警告这种行为。我想我曾经知道这一点,但多年来我已经忘记了。把它切换到
就成功了。它不仅解决了我使用其他方法遇到的问题,还解决了
搜索
中的问题。Do not use
my
with statement modifiers!The problem turned out to be in a called scope. Having forgotten the warning against using
my
with a statement modifier, I had coded the following:The first time it went through
@_
had one argument. It assigned the first value to$each
. The second time through, with more arguments it skipped themy
. So there was no declaration in the current scope, so it simply reused the sub that I had assigned the last time, and saved nothing in%params
because the$each
it referred to had a value.Weird, but as ysth pointed out perlsyn warns against this behavior. I think I used to know this, but have forgotten it over the years. Switching it to
did the trick. It not only cleaned up the problems I was having with another method, but it cleaned up problems in
search
.