Perl use strict "vars" 的问题
#! 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 -wuse strict "vars";our $newspaper;local $newspaper = "The Times";print "$newspaper";^DThe Times
[解决办法]
还有一点,一般用 "{}",即 左右花括号,来界定local的作用域。上面的左右花括号界定的区域也叫“块”。