2

在以下示例中,我对 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;
             }
        );
   });
...

然后我将它分配给 scalar $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;
4

1 回答 1

0

我在 perlmonks.org 中找到了答案:http ://perlmonks.org/?node_id=1047188

于 2013-08-21T05:04:51.430 回答