• 第四章 语句和声明


    <pre name="code" class="sql">$Some::stuff 使用包some的标准变量$stuff
    
    
    our 把名字限制于一个作用域,
    
    local 把值限制于一个作用域,
    
    my 把名字和值都限制于一个作用域
    
    
    
    my变量:
    [root@master Webqq]# cat t5.pl 
    {my $var=11;print "$var is 1--$var
    ";}{print "$var is 2--$var
    "};
    {our $var=11;print "$var is 1--$var
    ";}{print "$var is 2--$var
    "};
    [root@master Webqq]# perl t5.pl 
    $var is 1--11
    $var is 2--
    $var is 1--11
    $var is 2--11
    
    
    
    our变量:
    
    [root@master Webqq]# cat t6.pl 
    our $PROGRAM_NAME = "waiter";
    {
    my $PROGRAM_NAME = "something";
    print "$PROGRAM_NAME is 1---$PROGRAM_NAME
    ";
    our $PROGRAM_NAME = "server"; #这里的our和外面的相同,和前句不同。
    print "$PROGRAM_NAME is 2----$PROGRAM_NAME
    ";
    # 这里调用的代码看到的是"server"
     }
    print "$PROGRAM_NAME is 3----$PROGRAM_NAME
    ";
    [root@master Webqq]# perl t6.pl 
    $PROGRAM_NAME is 1---something
    $PROGRAM_NAME is 2----server
    $PROGRAM_NAME is 3----server
    [root@master Webqq]# 
    
    
    my 声明的是词法变量,"把名字和值都限于限于某个范围",简单说,就是只能本层模块或者函数可以看到这个变量,高一层的或
    
    者低一层的都看不到的。例如:
    sub greeting1{
    my ($hello) = "How are you do?";
    greeting2();
    }
    sub greeting2{
    print "$hello/n";
    }
    $hello = "How are you doing?";
    greeting2();
    greeting1();
    greeting2();
    运行结果:
    How are you doing?
    How are you doing?
    How are you doing?
    一个 How are you do? 都没有,在greeting1中call greeting2时,greeting2看不到greeting1的私有 $hello变量,只能看到外
    
    面的全局变量$hello
    
    
    
    如果你看到某人写到 $Some::stuff(注:或者 $Some'stuff,不过我们不
    鼓励这么写),他们是在使用来自包 Some 的标量变量 $stuff
    
    
    读取标准输入:
    
    [root@master Webqq]# cat t12.pl 
    my $foo = <STDIN>;
    print "$foo is $foo
    ";
    [root@master Webqq]# perl t12.pl 
    abc123
    $foo is abc123
    
    
    
    my  ,local ,our 变量:
    
    my 变量:
    [root@master Webqq]# cat t12.pl 
    my $i=10;
    {
    my $i=99;
    print "$i is 1--$i
    ";
    };
    print "$i is 2--$i
    ";
    [root@master Webqq]# perl t12.pl 
    $i is 1--99
    $i is 2--10
    
    
    our 全局变量:
    [root@master Webqq]# cat t12.pl 
    our $i=10;
    {
    our $i=99;
    print "$i is 1--$i
    ";
    };
    print "$i is 2--$i
    ";
    [root@master Webqq]# perl t12.pl 
    $i is 1--99
    $i is 2--99
    
    
    local 用来临时改变一个现有的全局变量的值
    
    [root@master Webqq]# cat t12.pl 
    our $i=10;
    {
    local  $i=99;
    print "$i is 1--$i
    ";
    };
    print "$i is 2--$i
    ";
    [root@master Webqq]# perl t12.pl 
    $i is 1--99
    $i is 2--10
    
    
    [root@master Webqq]# cat t12.pl 
    use strict;
    $i=10;
    [root@master Webqq]# perl t12.pl 
    Global symbol "$i" requires explicit package name at t12.pl line 2.
    Execution of t12.pl aborted due to compilation errors.
    
    
    4.6  全局声明:
    
    
    库,模块 和类都用包来存储它们的接口数据(以及一些它们的半私有数据)
    
    
    创建私有变量(my) ,进行有选择访问的全局变量(our)  给全局变量提供临时值的(local)
    
    
    4.7.3  词法范围全局声明:our
    
    有一个访问全局变量的更好的方法就是our 声明,尤其那些在use strice声明下运行的程序和模块。
    
    
    
    zjzc01:/root/big# cat a27.pl 
    {$var = aa;print "$var is $var
    "};
    print "$var is $var
    ";
    zjzc01:/root/big# perl a27.pl 
    $var is aa
    $var is aa
    
    
    zjzc01:/root/big# cat a27.pl 
    {my $var = aa;print "$var is $var
    "};
    print "$var is $var
    ";
    zjzc01:/root/big# perl a27.pl 
    $var is aa
    $var is 
    
    
    zjzc01:/root/big# cat a27.pl 
    {our $var = aa;print "$var is $var
    "};
    print "$var is $var
    ";
    zjzc01:/root/big# perl a27.pl 
    $var is aa
    $var is aa
    
    
    zjzc01:/root/big# cat a27.pl 
    {local $var = aa;print "$var is $var
    "};
    print "$var is $var
    ";
    zjzc01:/root/big# perl a27.pl 
    $var is aa
    $var is 
    
    
    
    4.7.4  动态范围变量:local
    
    zjzc01:/root/big# cat a28.pl 
    $var=100;
    {local $var=200; print "$var is $var
    "};
    print "$var is $var
    ";
    
    zjzc01:/root/big# perl a28.pl 
    $var is 200
    $var is 100
    
    这也是为什么我们称之为动态范围---因为它是在运行时修改的:
    
    尽管my和local 都提供了某种程度的保护,总的来说你还是应该优先使用my.
    
    
    当然,有时候你不得不用local来临时改变一个现有全局变量的值。
    
    
    
    
    
    
    


    
                                        
    
  • 相关阅读:
    机器学习中 margin loss 、hinge loss 和 ramp loss 的区别
    ML 论文中 用到的 temperature
    对一系列 pickle load 进行解包,只保留最后几个
    Linux 常用命令速览
    Numpy 的 dtype 和 astype 的区别
    远程配置 tensorflow 环境
    pytorch 的一些坑
    Conda 配置虚拟 pytorch 环境 和 Tensorflow 环境
    Ubuntu 远程离线配置 pytorch 运行环境
    3.Vue起步
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351373.html
Copyright © 2020-2023  润新知