zoukankan      html  css  js  c++  java
  • perl 回调函数

    在计算机程序设计中,回调函数,或简称回调(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
    ";
    };
    
    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
    ";
    };
    
    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
    ";
    };
    
    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.

  • 相关阅读:
    系统分析与设计——WordCount
    ruby学习笔记
    RubyMine 2016.1 下载 附注册激活码 破解版方法
    Java环境一键配置,需要手动输入jdk路劲。
    iOS学习资源收集
    TPC-H数据导入Hive方案
    Navicat连接不上远程主机数据库的问题解决方案
    TPC-H数据导入Mysql数据方案
    2018/09/26 LINUX安装及linux命令之ls命令学习
    使用Xcode编写并运行Python
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350770.html
Copyright © 2011-2022 走看看