zoukankan      html  css  js  c++  java
  • 子过程左值属性

    <pre name="code" class="html">除非你定义子过程返回一个 左值,否则你你不能从子过程中返回一个可以修改的标量值:
    [root@wx03 test]# cat t20.pl 
    my $val;
    sub canmod:lvalue  {
    return $val;
    }
    sub nomod {
    $val;
    }
    canmod() = 5; # 给 $val 赋值为 5
    #nomod() = 5; # 错误
    print "$val is $val
    ";
    print "1111111111111
    ";
    print &canmod;
    print "
    ";
    
    [root@wx03 test]# perl t20.pl 
    $val is 5
    1111111111111
    5
    
    
    修改子程序的返回值
    
    
    如果你正传递参数到一个有 左值 属性的子过程,你一般会使用圆括弧来防止歧义:
    [root@wx03 test]# cat t20.pl 
    my $val;
    sub canmod:lvalue  {
    $val =shift;
    return $val;
    }
    sub nomod {
    $val;
    }
    canmod($x) = 100; # 给 $val 赋值为 5
    #nomod() = 5; # 错误
    print "$val is $val
    ";
    
    [root@wx03 test]# perl t20.pl 
    $val is 100
    
    具有左值属性的方法调用在 不传送任何参数时也能省
    略圆括弧:
    $obj->canmod = 5;
    
    
    12.7.6 新技巧
    
    package Critter;
    sub new {
    my $class = shift;
    my $self = { pups => 0, @_ }; # 覆盖缺省。
    bless $self, $class;
    }
    sub pups : lvalue { # 我们稍后给pups()赋值
    my $self = shift;
    $self->{pups};
    }
    package main;
    $varmint = Critter->new(pups => 4);
    $varmint->pups *= 2; # 赋给 $varmint->pups!  这里把8赋值给$varmint->pups
    $varmint->pups =~ s/(.)/$1$1/; # 现场修改 $varmint->pups!
    print $varmint->pups; # 现在我们有88个pups。
    
    
    
    
    [root@wx03 test]# cat t22.pl 
    unshift(@INC,"/root/test"); 
    use loop;
    $ua=loop->new();
    $str=$ua->test_fun1;
    print "$str is $str
    ";
    [root@wx03 test]# perl t22.pl 
    ok
    $str is 1
    
    
    $str 是函数的返回值,这里返回1
    


    
       
    
    
  • 相关阅读:
    从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。
    C#开发者工具网
    sqlitestudio
    jstat命令 Java Virtual Machine Statistics Monitoring Tool
    ProxyPass与ProxyPassReverse及ProxyPassMatch的概述
    IBM MQ介绍
    sun misc unsafe类的使用
    Android Webview实现文件下载功能
    使用OpenSSL生成私钥 Private Key 以及根据Private Key创建证书
    阿里巴巴的面试
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200007.html
Copyright © 2011-2022 走看看