perl 函数回调 引用$client->run(sub {$client->sync});

来源:互联网 发布:黑客帝国4矩阵复活 云 编辑:程序博客网 时间:2024/05/11 01:59
匿名函数引用:[root@wx03 wx]# perl a1.pl CODE(0x2077b30)test[root@wx03 wx]# cat a1.pl $ref= sub {return test."\n"};print $ref;print "\n";print &$ref;[root@wx03 wx]# perl a1.pl CODE(0x21a2b30)testmy $eventloop=$client->run(sub {print "test\n";});  sub {print "test\n";} 是匿名函数一个引用;[root@wx03 lib]# cat eventloop.pm package eventloop;use AE;   use AnyEvent;##定义watch    sub run {my $self=shift;       my $code=shift;    my $t = AnyEvent->timer(        after    => 0,        interval => $self->{interval},        cb       => $code    );    ##不要再每秒打印时间    ##undef $t;    my $cv = AnyEvent->condvar;    $cv->recv;};1;cb=> 必须是函数的引用:[root@wx03 wx]# cat scan.pl use lib '/root/scanwx/lib';use Client;use Data::Dumper;my $client=Client->new();my $qrcode=$client->get_qrcode();print "\$qrcode is $qrcode\n";my $login_info=$client->login_info();my $login=$client->login();$client->run(sub {$client->sync});$str=Dumper($client);print "\$str is $str\n"[root@wx03 lib]# cat sync.pm package sync;sub sync {   my $self=shift;   print "test\n";};1;

0 0
原创粉丝点击