Perl use strict "vars" 的问题

liujunyu 2010-04-04 01:42:37
#! 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 都是可以正确运行的?
...全文
204 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sikinzen 2010-04-06
  • 打赏
  • 举报
回复
还有一点,一般用 "{}",即 左右花括号,来界定local的作用域。上面的左右花括号界定的区域也叫“块”。

[Quote=引用 1 楼 fibbery 的回复:]
local并不是用来声明局部变量的,声明局部变量的事情是my来做的;local是用来将一个已经存在的全局变量局部化的,所以,使用strict的时候,local局部化的变量必须要事先声明。

Perl code
C:\Documents and Settings\fibbery>perl
#! c:/perl/bin -w
use strict "vars";
our $newspape……
[/Quote]
fibbery 2010-04-04
  • 打赏
  • 举报
回复
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

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧