perl – 为什么会选择在单独的语句中声明和初始化一个词汇变量?
发布时间:2020-12-15 21:13:39 所属栏目:大数据 来源:网络整理
导读:这是从 AnyEvent::Intro 摘录 # register a read watchermy $read_watcher; $read_watcher = AnyEvent-io ( fh = $fh,poll = "r",cb = sub { my $len = sysread $fh,$response,1024,length $response; if ($len = 0) { # we are done,or an error occurred,l
这是从
AnyEvent::Intro 摘录
# register a read watcher my $read_watcher; $read_watcher = AnyEvent->io ( fh => $fh,poll => "r",cb => sub { my $len = sysread $fh,$response,1024,length $response; if ($len <= 0) { # we are done,or an error occurred,lets ignore the latter undef $read_watcher; # no longer interested $cv->send ($response); # send results } },); 为什么使用 my $read_watcher; $read_watcher = AnyEvent->io (... 代替 my $read_watcher = AnyEvent->io (... ? 解决方法
因为闭包引用$read_watcher,并且$read_watcher解析为词法的范围只从包含我的语句开始.
这是有意的,所以这样的代码指的是两个单独的变量: my $foo = 5; { my $foo = $foo; $foo++; print "$foon"; } print "$foon"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |