我的perl脚本是:
#!/usr/bin/perl -w
use strict ;
use warnings;
print "Hello $name \n"
我收到此错误:
全局符号“$ name”需要fst_pscpt.pl上的显式包名称。
这真的阻止了我的进步。我们需要包括任何包???
谢谢&问候, B.Raviteja
答案 0 :(得分:13)
您尚未声明任何变量$name
。所以你需要以某种方式获得该变量。例如,如果您想从命令行获取它,则可以执行以下操作:
#!/usr/bin/perl -w
use strict;
use warnings;
my $name = $ARGV[0];
print "Hello, $name!\n";
然后像这样调用你的程序:
./myprog.pl Rafe
获得输出:
Hello, Rafe!
此外,您在最后一行的末尾没有分号。你也需要它。
答案 1 :(得分:5)
diagnostics为您提供了更多有用的帮助:
$ perl -Mdiagnostics fst_pscpt.pl
Global symbol "$name" requires explicit package name at fst_pscpt.pl line 4.
Execution of fst_pscpt.pl aborted due to compilation errors (#1)
(F) You've said "use strict" or "use strict vars", which indicates
that all variables must either be lexically scoped (using "my"),
declared beforehand using "our", or explicitly qualified to say
which package the global variable is in (using "::").
Uncaught exception from user code:
Global symbol "$name" requires explicit package name at fst_pscpt.pl line 4.
Execution of fst_pscpt.pl aborted due to compilation errors.
at fst_pscpt.pl line 5
答案 2 :(得分:2)
您需要在$name
(包括严格的变量)时声明use strict;
。只需插入一行:
my $name;
使用前。