#! c:/perl/bin -w
use strict "vars";
local $newspaper = "The Times";
print "$newspaper";
#会报错.
Global symbol "$newspaper" requires explicit package name at testing.pl line 3.
Global symbol "$newspaper" requires explicit package name at testing.pl line 4.
testing.pl had compilation errors.
如果把local $newspaper 换成:
my $newspaper 或者 our $newspaper 都是可以正确运行的.
这个原因是由于
local并不是用来声明局部变量的,声明局部变量的事情是my来做的;local是用来将一个已经存在的全局变量局部化的,所以,使用strict的时候,local局部化的变量必须要事先声明。
C:\Documents and Settings\fibbery>perl
#! c:/perl/bin -w
use strict "vars";
our $newspaper;local $newspaper = "The Times";
print "$newspaper";
^D
The Times
还有一点,一般用 "{}",即 左右花括号,来界定local的作用域。上面的左右花括号界定的区域也叫“块”。
以后使用需要注意!!!!!!
阅读(1136) | 评论(0) | 转发(0) |