2010-11-23 50 views
9

I'm写我在Perl程序首先,写下这样的:为什么Perl说全局符号“SYMBOL”需要在PROGRAM.pl行X处显式包名?

use strict; 
use warnings; 
$animal = "camel"; 
print($animal); 

当我运行它,我从Windows命令行这些消息:

Global symbol "animal" requires explicit package name at stringanimal.pl line 3 
Global symbol "animal" requires explicit package name at stringanimal.pl line 4 

请,任何人都可以这些消息是什么意思?

回答

25

use strict;强制你在使用它们之前声明你的变量。如果你不(如你的代码示例),你会得到这个错误。

来声明变量,改变这一行:

$animal = "camell"; 

要:

my $animal = "camell"; 

请参阅 “Declaring variables” 进行了较为深入的解释,也是的Perldoc节use strict

P.S.骆驼拼写“骆驼” :-)

编辑:什么错误消息,实际上意味着是Perl不能找到一个名为$animal,因为它并没有被声明的变量,并假定它必须是一个变量定义在包中,但是你忘记在包名前面加上前缀,例如$packageName::animal。显然,这里并不是这样,你根本没有申报$animal

+0

¿什么是包装? – Peterstone 2010-11-23 15:05:33

0

你必须把:

my $animal = "camel" 

使用use strict时。

相关问题