2010-11-23 18 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時。

相關問題