为什么 Perl 说全局符号“SYMBOL"?在 PROGRAM.pl 第 X 行需要明确的包名? [英] Why does Perl say Global symbol "SYMBOL" requires explicit package name at PROGRAM.pl line X?
问题描述
我正在用 Perl 编写我的第一个程序,并写道:
I´m writing my first programs in Perl, and wrote this:
use strict;
use warnings;
$animal = "camel";
print($animal);
当我运行它时,我从 Windows 命令行收到这些消息:
When I run it, I get these messages from the Windows command-line:
Global symbol "animal" requires explicit package name at stringanimal.pl line 3
Global symbol "animal" requires explicit package name at stringanimal.pl line 4
请问,有人知道这些消息是什么意思吗?
Please, could anyone what these messages mean?
推荐答案
use strict;
强制您在使用变量之前声明它们.如果您不这样做(如您的代码示例中所示),您将收到该错误.
use strict;
forces you to declare your variables before using them. If you don't (as in your code sample), you'll get that error.
要声明变量,请更改此行:
To declare your variable, change this line:
$animal = "camell";
致:
my $animal = "camell";
请参阅声明变量"以获得更深入的解释,以及use strict
的 Perldoc 部分.
See "Declaring variables" for a more in-depth explanation, and also the Perldoc section for use strict
.
附言骆驼拼写为骆驼":-)
P.S. Camel is spelt "camel" :-)
错误信息的实际意思是 Perl 找不到名为 $animal
的变量,因为它没有被声明,并假设它必须是一个在包中定义的变量,但你忘记在它前面加上包名,例如$packageName::animal
.显然,这里不是这种情况,您只是没有声明 $animal
.
What the error message actually means is that Perl can't find a variable named $animal
since it hasn't been declared, and assumes that it must be a variable defined in a package, but that you forgot to prefix it with the package name, e.g. $packageName::animal
. Obviously, this isn't the case here, you simply hadn't declared $animal
.
这篇关于为什么 Perl 说全局符号“SYMBOL"?在 PROGRAM.pl 第 X 行需要明确的包名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!