在以下示例中,我对Perl中的变量垃圾收集有点混淆:
#!/bin/env perl
use v5.14;
package MyTestModule {
sub foo {
my $fh = shift;
for(1..100){
say "PUT";
$fh->autoflush(1);
print $fh "Heloo\n";
sleep 1;
}
}
}
package main;
use AnyEvent;
use AnyEvent::Fork::Template;
my $cv = AnyEvent->condvar;
$AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub {
my $fh_fh_fh = shift;
my $w_w_w;
$w_w_w = AnyEvent->io(fh => $fh_fh_fh, poll => "r", cb => sub {
$w_w_w unless 1;
sysread $fh_fh_fh, my $rslt, 10;
say "GOT:", $rslt;
}
);
});
$cv->wait;
在上面的代码中,如果我删除了$w_w_w
,那么由于AnyEvent->io
创建了一个对象,它的引用变为零,然后它将被Perl回收,这使得代码不起作用( cb不会被调用);
...
$AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub {
my $fh_fh_fh = shift;
AnyEvent->io(fh => $fh_fh_fh, poll => "r", cb => sub {
sysread $fh_fh_fh, my $rslt, 10;
say "GOT:", $rslt;
}
);
});
...
然后我将它分配给标量$w_w_w
,但我不需要回调中的$w_w_w
。但以下代码不起作用(不会调用回调):
$AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub {
my $fh_fh_fh = shift;
my $w_w_w;
$w_w_w = AnyEvent->io(fh => $fh_fh_fh, poll => "r", cb => sub {
sysread $fh_fh_fh, my $rslt, 10;
say "GOT:", $rslt;
}
);
});
所以我在回调中添加了一行代码(只是你可以在这个请求的前面看到它):
$ w_w_w除非1;
因为断言总是假的,所以它不会运行,我认为perl编译器/解释器会优化这个行代码(删除它),然后$ w_w_w的引用计数将为零。并且也不会调用回调。 但它的确有效。所以我希望有人可以解释一下。(以下是Perl -ODeparse的转储
perl -MO = Deparse t2.pl
sub BEGIN {
require v5.14;
}
package MyTestModule;
sub foo {
use strict;
no feature;
use feature ':5.12';
my $fh = shift();
foreach $_ (1 .. 100) {
say 'PUT';
$fh->autoflush(1);
print $fh "Heloo\n";
sleep 1;
}
}
package main;
use strict;
no feature;
use feature ':5.12';
{;};
use AnyEvent;
use AnyEvent::Fork::Template;
my $cv = 'AnyEvent'->condvar;
$AnyEvent::Fork::Template->fork->run('MyTestModule::foo', sub {
my $fh_fh_fh = shift();
my $w_w_w;
$w_w_w = 'AnyEvent'->io('fh', $fh_fh_fh, 'poll', 'r', 'cb', sub {
'???';
sysread $fh_fh_fh, my $rslt, 10;
say 'GOT:', $rslt;
}
);
}
);
$cv->wait;