zoukankan      html  css  js  c++  java
  • 引用

    在 C 里,间接的最常见的形式就是指针,它
    可以让一个变量保存另外一个变量的内存地址。在 Perl 里,间接的最常见的形式是引用。
    
    举例来说,假如你创建了一个指向某个词法
    范围数组 @array 的硬引用。那么,即使在 @array 超出了范围之后,该引用以及它所
    引用的参考物也仍然继续存在。一个引用只有在对它的所有引用都消失之后才会被摧毁。
    
    8.2 创建引用:
    
    8.2.2 匿名数据
    
    
    匿名数组组合器:
    
    你可以用方括弧创建一个创建一个指向匿名数组的引用:
    [root@wx03 3]# cat t5.pl 
    $arrayref = [1, 2, ['a', 'b', 'c', 'd']];
    print $arrayref->[2]->[2];
    print "
    ";
    
    [root@wx03 3]# perl t5.pl 
    c
    
    
    
    [root@wx03 3]# cat t6.pl 
    $table = [ [ "john", 47, "brown", 186], [ "mary", 23, "hazel", 128], [ "bill", 35, "blue", 157] ];
    print $table->[1]->[0];
    print "
    ";
    [root@wx03 3]# perl t6.pl 
    mary
    
    
    匿名散列组合器:
    
    [root@wx03 3]# cat t7.pl 
    $hashref = {
    'Adam' => 'Eve',
    'Clyde' => $bonnie,
    'Antony' => 'Cleo' . 'patra',
    };
    
    print $hashref->{Antony};
    print "
    ";
    [root@wx03 3]# perl t7.pl 
    Cleopatra
    
    [root@wx03 3]# cat t8.pl 
    $table = {
    "john" => [47, "brown", 186],
    "mary" => [23, "hazel", 128],
    "bill" => [35, "blue", 157],
    };
    print $table->{mary}->[2];
    print "
    ";
    [root@wx03 3]# perl t8.pl 
    128
    
    [root@wx03 3]# cat t9.pl 
    $table = {
    "john" => { age => 47,
    eyes => "brown",
    weight => 186,
    },
    "mary" => { age => 23,
    eyes => "hazel",
    weight => 128,
    },
    "bill" => { age => 35,
    eyes => "blue",
    weight => 157,
    },
    213
    };
    
    print $table->{mary}{eyes};
    print "
    ";
    [root@wx03 3]# perl t9.pl 
    hazel
    
    
    匿名子过程组合器:
    
    你可以通过用不带子过程名字的 sub 创建一个匿名子过程:
    
    [root@wx03 3]# cat t10.pl 
    $coderef = sub { print "Boink!
    " };
    print &$coderef;
    print "
    ";
    [root@wx03 3]# perl t10.pl 
    Boink!
    1
    
    对象构造器:
    特别是那些叫构造器的特殊子过程创建并返回指向对象的
    引用。
    [root@wx03 3]# cat t11.pl 
    $var="abc123";
    $scalarref=$var;
    print $scalarref;
    print "
    ";
    
    $bar = ${$scalarref};
    print $bar;
    print "
    ";
    [root@wx03 3]# perl t11.pl 
    SCALAR(0x1486b18)
    abc123
    
    把一个 BLOCK 块当作变量名用:
    
    使用箭头操作符:
    
    使用对象方法:
    
    [root@wx03 3]# cat t12.pl 
    $john = [ {age => 1, eyes => 2, weight => 3}, 47, "brown", 186 ];
    print $john->[0]->{eyes};
    print "
    ";
    
    
    [root@wx03 3]# perl t12.pl 
    2
    
    
    闭合(闭包):
    
    一个指向一个词法变量的引用:
    
    [root@wx03 3]# cat t13.pl 
    {
    my $critter = "camel";
    $critterref = $critter;
    print "first.........
    ";
    print "$critter is $critter
    ";
    };
    
    print  "second............
    ";
    print $$critterref;
    print "
    ";
    
    
    [root@wx03 3]# perl t13.pl 
    first.........
    $critter is camel
    second............
    camel
    
    
    $$critterref 的数值仍将是“camel”,即使在离开闭合的花括弧之后 $critter 消失了也如
    此。
    
    
    $critterref 也可以指向一个指向了 $critter 的子过程:
    
    [root@wx03 3]# cat t14.pl 
    {
    my $critter = "camel";
    $critterref = sub { return $critter };
    print "1---------
    ";
    print &$critterref;
    print "
    ";
    };
    print "2---------
    ";
    print &$critterref;
    print "
    ";
    
    [root@wx03 3]# perl t14.pl 
    1---------
    camel
    2---------
    camel
    
    这是一个闭合(闭包)
    
    
    闭合的另外一个用途就是函数生成器,也就是说,创建和返回全新函数的函数。下面是一个
    用闭合实现的函数生成器的例子:
    
    
    嵌套的子过程:
    

  • 相关阅读:
    maven编译时GBK错误
    生产环境中,通过域名映射ip切换工具SwitchHosts
    Java中的Filter过滤器
    Notepad++远程连接Linux系统
    MySQL 创建帐号和对表的导入导出
    XML 初识
    MySQL 字符集的设置
    C# 委托
    肖申克的救赎
    C#指针 常用手段
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199984.html
Copyright © 2011-2022 走看看