在计算机程序设计中,回调函数,或简称回调(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.
perl 回调函数
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:子过程左值属性
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章