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.

  • 相关阅读:
    Python-快速入门
    Python-面向对象编程
    python-模块
    .net mvc onexception capture; redirectresult;
    a c lang in linux
    上海哪里有学陈氏太极拳?
    【Origin】 叹文
    【Origin】 碑铭
    【Origin】 偶题 之 抒意
    【Origin】答友朋关切书
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350770.html
Copyright © 2011-2022 走看看