zoukankan      html  css  js  c++  java
  • 第六章 子过程 和子过程引用

    <pre name="code" class="python">第六章   子过程:
    
       象其他的语言一样,Perl也支持自定义的子程序。(注: 我们也把它们叫做函数,不过,函数和子程序在Perl里是一样的东西。
    
    有时候我们甚至叫它们方法.
    
    @_ 存储的是数组参数
    @_  $_[0] 和$_[1]
    
    [oracle@jhoa 20150319]$ cat a3.pl 
    sub max {
    $max = shift(@_);
    for my $item (@_) {
    $max = $item if $max < $item;
    }
    return $max;
    }
    $bestday = max(20, 8, 12, 16, 30);
    print "$bestday is $bestday 
    ";
    
    [oracle@jhoa 20150319]$ perl a3.pl 
    $bestday is 30 
    
    2.3 范围问题:
    
    &foo(1,2,3) # 传递三个参数
    foo(1,2,3) # 和上面一样
    foo(); # 传递一个空列表
    &foo(); # 和上面一样
    &foo; # foo() 获取当前的参数,和 foo(@_) 一样,但更快!
    foo; # 如果预定义了子过程 foo,那么和 foo() 一样,否则
    # 就是光字 "foo"
    
    
    [oracle@jhoa 20150319]$ cat a4.pl 
    sub max {
    print "this is test
    ";
    };
    
    $subref =  max;
    print "$subref  is $subref 
    ";
    
    [oracle@jhoa 20150319]$ perl a4.pl 
    this is test
    $subref  is 1 
    
    获取函数返回值:
    
    间接调用子过程(通过名字或引用),可以使用下面的任何一种方法:
    
    1. &$subref(LIST) # 在间接调用的时候,& 不能忽略
    
    2. $subref->(LIST) # (除非使用中缀表示法)
    
    3. &$subref # 把当前的 @_ 输出到该子过程
    
    
    $subref = &name 来获取一个命名子过程的
    引用的时候
    
    [oracle@jhoa 20150319]$ cat a4.pl 
    sub max {
    print "this is test
    ";
    };
    
    $subref =  max;
    print "$subref  is $subref 
    ";
    $ref = &max;
    print "$ref is $ref
    ";
    
    [oracle@jhoa 20150319]$ perl a4.pl 
    this is test
    $subref  is 1 
    $ref is CODE(0x1def7080)
    
    &max 函数的引用:
    
    [oracle@jhoa 20150319]$ cat a4.pl 
    sub max {
    print "this is test
    ";
    };
    
    $subref =  max;
    print "$subref  is $subref 
    ";
    $ref = &max;
    print "$ref is $ref
    ";
    print "1-------
    ";
    &$ref();
    
    [oracle@jhoa 20150319]$ perl a4.pl 
    this is test
    $subref  is 1 
    $ref is CODE(0x1e73e080)
    1-------
    this is test
    
    
    &$ref(); ---调用函数的引用
    
    
    $subref = &name 来获取一个命名子过程的引用的时候
    
    
    3.0 传入引用:
    [oracle@jhoa 20150319]$ cat t3.pl 
    @a=qw/1 2 3 4 5 6 7/;
    sub sum {
           my $aref = shift @_;
          foreach (@{$aref}) { $total += $_};
          return  $total;
    };
    
    $var = sum (@a);
    print "$var is $var
    ";
    [oracle@jhoa 20150319]$ perl t3.pl 
    $var is 28
    
    
    $subref = ∑ 子程序的引用:
    
    &{$subref} 和 $subref->()
    
    
    [oracle@jhoa 20150319]$ cat t3.pl 
    @a=qw/1 2 3 4 5 6 7/;
    sub sum {
           my $aref = shift @_;
          foreach (@{$aref}) { $total += $_};
          return  $total;
    };
    $subref = ∑
    print "$subref is $subref
    ";
    $var = &{$subref}(@a);
    print "$var is $var
    ";
    [oracle@jhoa 20150319]$ perl t3.pl 
    $subref is CODE(0xc2fbe90)
    $var is 28
    
    ----------------------------------------
    [oracle@jhoa 20150319]$ cat t3.pl 
    @a=qw/1 2 3 4 5 6 7/;
    sub sum {
           my $aref = shift @_;
          foreach (@{$aref}) { $total += $_};
          return  $total;
    };
    $subref = ∑
    print "$subref is $subref
    ";
    $var = &$subref(@a);
    print "$var is $var
    ";
    [oracle@jhoa 20150319]$ perl t3.pl 
    $subref is CODE(0xa623e90)
    $var is 28
    
    
    
    
    &{$subref}    调用子程序的引用
    
    [oracle@jhoa 20150319]$ cat t3.pl 
    @a=qw/1 2 3 4 5 6 7/;
    sub sum {
           my $aref = shift @_;
          foreach (@{$aref}) { $total += $_};
          return  $total;
    };
    $subref =  &sum
    print "$subref is $subref
    ";
    
    $a=$subref->(@a);
    print "$a is $a
    ";
    [oracle@jhoa 20150319]$ perl t3.pl 
    $subref is CODE(0xb833e90)
    $a is 28
    
    #################################################
    子程序声明:
    
    4.1 内联常量函数
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    
    
    
                                        
    
  • 相关阅读:
    常用Linux命令
    SQL必知必会-笔记
    【ubuntu】install openbox+tint2+bmenu on ubuntu12.04.4
    【ruby】安装Ruby
    【ruby】快速安装gems的方法
    【sinatra】设置默认的端口
    【sinatra】修改默认ip绑定
    【sinatra】结合Padrino framework
    【sinatra】安装测试
    【rails3教材】博客构建过程2
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351811.html
Copyright © 2011-2022 走看看