我的第一个perl脚本正在生成一个错误

我的第一个perl脚本正在生成一个错误,perl,Perl,我的perl脚本是: #!/usr/bin/perl -w use strict ; use warnings; print "Hello $name \n" 我得到这个错误: 全局符号“$name”要求在fst_pscpt.pl处显示包名 这真的阻止了我的进度。我们需要包括任何包吗 谢谢和问候, B.Raviteja您尚未声明任何变量$name。所以你需要以某种方式得到这个变量。例如,如果要从命令行获取,可以执行以下操作: #!/usr/bin/perl -w use strict; use

我的perl脚本是:

#!/usr/bin/perl -w
use strict ;
use warnings;
print "Hello $name \n"
我得到这个错误:

全局符号“$name”要求在fst_pscpt.pl处显示包名

这真的阻止了我的进度。我们需要包括任何包吗

谢谢和问候,
B.Raviteja

您尚未声明任何变量
$name
。所以你需要以某种方式得到这个变量。例如,如果要从命令行获取,可以执行以下操作:

#!/usr/bin/perl -w
use strict;
use warnings;
my $name = $ARGV[0];
print "Hello, $name!\n";
然后像这样调用您的程序:

./myprog.pl Rafe
并获得输出:

Hello, Rafe!

另外,最后一行的末尾没有分号。你也需要它。

当你
使用strict时,你需要声明
$name
(包括严格的变量)。只需插入以下行:

my $name;
使用前。

在这种情况下为您提供了更有用的帮助:

$ 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

s/ARGV[1]/ARGV[0]/
。分号在块的末尾是可选的,包括代码的结尾。Perl在这方面和其他许多方面都非常宽容。不过,始终以分号结束语句是一个好习惯。我同意,而且几乎总是使用可选分号。但是,如果是可选的,你需要的是对事实来说有点太强的。同意。Perl中的分号是语句分隔符,而不是语句终止符。在块/程序的末尾有一个是一个好习惯,但你不需要它。当你有
使用警告时,没有必要使用
-w
标志在您的代码中<代码>使用警告
被认为是正确的现代用法。有关这方面的更多信息,请阅读perllexwarn。在Perl学习的这一阶段,这可能没有多大意义,但您应该得到一个链接,以防您不想盲目地将我的断言当作事实。示例中很好的一部分是,您已经从第一个scriptWell开始遵循Perl最佳实践。您还需要将其设置为除
unde
之外的其他值,否则
print
将无法正常工作。打印将正常工作。不过,您会因为没有初始化$name而受到警告。。。