在计算机程序设计中,回调函数,或简称回调(Callback),是指通过函数参数传递到其它代码的,某一块可执行代码的引用。这一设计允许了底层代码调用在高层定义的子程序。
没啥不好理解的呀,就是向函数的参数为 一个函数的引用呀。。
[root@wx03 ~]# cat a2.pl
use AE;
use AnyEvent;
##定义watch
sub run {
my $code=shift;
my $t = AnyEvent->timer(
after => 0,
interval => 1,
cb => $code,
);
my $cv = AnyEvent->condvar;
$cv->recv;
};
sub test {
print "11111111111\n";
};
run(sub {test()});
[root@wx03 ~]# perl a2.pl
11111111111
11111111111
[root@wx03 ~]# cat a2.pl
use AE;
use AnyEvent;
##定义watch
sub run {
my $code=shift;
my $t = AnyEvent->timer(
after => 0,
interval => 1,
cb => $code,
);
my $cv = AnyEvent->condvar;
$cv->recv;
};
sub test {
print "11111111111\n";
};
run(\&test);
[root@wx03 ~]# perl a2.pl
11111111111
11111111111
[root@wx03 ~]# perl a2.pl
11111111111
1: callback must be a CODE reference or another callable object at /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/AnyEvent/Impl/EV.pm line 50.
[root@wx03 ~]# cat a2.pl
use AE;
use AnyEvent;
##定义watch
sub run {
my $code=shift;
my $t = AnyEvent->timer(
after => 0,
interval => 1,
cb => $code,
);
my $cv = AnyEvent->condvar;
$cv->recv;
};
sub test {
print "11111111111\n";
};
run(&test);
[root@wx03 ~]# perl a2.pl
11111111111
1: callback must be a CODE reference or another callable object at /usr/local/perl/lib/site_perl/5.22.1/x86_64-linux/AnyEvent/Impl/EV.pm line 50.