免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3442 | 回复: 6

perl的local问题。 [复制链接]

论坛徽章:
0
发表于 2008-05-05 17:27 |显示全部楼层
##############################
my $hello = "How are you doing?";
sub test{
    local $hello = "insub";
    print $hello;
}
test();
这个出错可以理解,因为对已经用my声明的变量 不能再用local声明了。

不明白的是这个
############

sub test1{
    local $hello = "insub";
    print $hello;
}
my $hello = "How are you doing?";
test1();

为什么这样写就ok了呢?

论坛徽章:
0
发表于 2008-05-05 18:00 |显示全部楼层
原帖由 shake863 于 2008-5-5 17:27 发表
##############################
my $hello = "How are you doing?";
sub test{
    local $hello = "insub";
    print $hello;
}
test();
这个出错可以理解,因为对已经用my声明的变量 不能再用local声 ...

第2个程序你加上 use strict; 也会报错。

论坛徽章:
0
发表于 2008-05-05 18:22 |显示全部楼层
原帖由 shake863 于 2008-5-5 17:27 发表
##############################
my $hello = "How are you doing?";
sub test{
    local $hello = "insub";
    print $hello;
}
test();
这个出错可以理解,因为对已经用my声明的变量 不能再用local声 ...

你需要明白
1.

  1. $a = "att";
  2. my $a = "btt";
复制代码

这样是定义了两个变量,第一个是global的,第二个是lexicall的
2.

  1. my $a ="att";
复制代码

这样只定义了一个变量


引用effective perl programming中的例子

  1. my $compile_time;
  2. $compile_time;
  3. print join " ", keys (%::);

  4. $compile_time; #actually create two variables
  5. my $compile_time;
  6. print join " ", keys (%::);
复制代码
What these examples demonstrate is that my variables do not "live" in the package symbol tables. In the example with my $compile_time first, there is only one variable named $compile_time in the file, and it never gets into the package symbol table. In the other example, there are two separate variables named $compile_time : the global one in the symbol table, and my $compile_time , which is not in a symbol table.



你可以执行下代码,自己验证下

回到你的问题
由于你的sub test1 definition在前
所以首先创建了一个全局变量$hello (local只能用于全局的变量,用my定义的不存在于package 的symbol table中)
然后后来又创建了一个局部变量$hello
所以共有两个变量
执行函数的时候调用的是全局那个变量


建议弄清楚my local use的区别
同时最好加上use strict;
这样可以避免很多错误

引用programming perl中chapter 4的话
my ($nose, @eyes, %teeth);
our ($House, @Autos, %Kids);
local (*Spouse, $phone{HOME});
Each of these modifiers offers a different sort of "confinement" to the variables they modify. To oversimplify slightly: our confines names to a scope, local confines values to a scope, and my confines both names and values to a scope.
Each of these constructs may be assigned to, though they differ in what they actually do with the values, since they have different mechanisms for storing values. They also differ somewhat if you don't (as we didn't above) assign any values to them: my and local cause the variables in question to start out with values of undef or (), as appropriate; our, on the other hand, leaves the current value of its associated global unchanged.

Repeated our declarations do not meaningfully nest. Every nested my produces a new variable, and every nested local a new value. But every time you use our, you're talking about the same global variable, irrespective of nesting. When you assign to an our variable, the effects of that assignment persist after the scope of the declaration. That's because our never creates values; it just exposes a limited form of access to the global, which lives forever:
our $PROGRAM_NAME = "waiter";
{
    our $PROGRAM_NAME = "server";
    # Code called here sees "server".
    ...
}
# Code executed here still sees "server".

[ 本帖最后由 churchmice 于 2008-5-5 18:34 编辑 ]

论坛徽章:
0
发表于 2008-05-05 18:34 |显示全部楼层
先不考虑“use strict;”的问题,

上面两段代码的区别只是 my $hello = "How are you doing?";
声明的位置不同。

我想知道的是,为什么下一段能出结果。

论坛徽章:
0
发表于 2008-05-05 18:47 |显示全部楼层
原帖由 shake863 于 2008-5-5 18:34 发表
先不考虑“use strict;”的问题,

上面两段代码的区别只是 my $hello = "How are you doing?";
声明的位置不同。

我想知道的是,为什么下一段能出结果。

你显示的指明local的是全局变量也是没有问题的

  1. #!/usr/bin/perl
  2. my $hello = "initialized outside sub";
  3. sub test1{
  4.         local $::hello = "initialized inside sub";
  5.         print "Global variable $::hello \n";
  6.         print "Lexical variable $hello \n"
  7.         }
  8. test1 ();
复制代码

<lig@romeo:~/chinaunix>$ ./value
Global variable initialized inside sub
Lexical variable initialized outside sub


如果看了上贴,这个就很好理解了

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2008-05-05 18:49 |显示全部楼层
这样就可以了:
  1. local $::hello = "insub";
复制代码

论坛徽章:
0
发表于 2008-05-05 21:40 |显示全部楼层
谢谢 churchmice 和 flw 的热情。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP