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

匿名函数引用:

[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)
test



my $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;





posted @ 2016-05-19 17:21  czcb  阅读(151)  评论(0编辑  收藏  举报