Perl Hello World无效

时间:2016-08-03 10:21:42

标签: perl

我刚开始学习perl。我编写了一个Hello World程序 - hello.pl并使用'+ x'使其可执行。

我可以使用perl hello.pl执行它,但是当我尝试./hello.pl时,会出现错误:错误:没有这样的文件“Hello World”

是什么原因?

编辑:

我的节目

use warnings;
use strict;
use 5.010;

print "Hello World";

我的错误:

  

./ hello.pl:line 1:use:找不到命令   ./hello.pl:line 2:use:找不到命令   ./hello.pl:line 3:use:找不到命令   错误:没有这样的文件“Hello World”

2 个答案:

答案 0 :(得分:9)

在程序顶部使用shebang line。它告诉内核或Apache将该文件解释为Perl脚本。并应将use warnings;use strict;放在程序的顶部

#!/usr/bin/perl
use warnings;
use strict;

print "hello world";

更多关于shebang

Does the shebang determine the shell which runs the script?

Why should the shebang line always be the first line?

答案 1 :(得分:2)

没有看到你的代码,这只是一个猜测(为什么人们认为我们可以调试他们的程序而不看他们?)但你的程序中是否有正确的引号字符?

你的程序应该是这样的:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

say 'Hello World';

或者(使用旧版本的Perl)像这样:

#!/usr/bin/perl

use strict;
use warnings;

print "Hello World\n";

我的第一个版本在字符串周围使用单引号字符,而我的第二个版本使用双引号字符。如果您收到错误,说“#34;没有这样的文件"那么你似乎有可能使用反引号 - 用于执行外部程序。您的print行是否如下所示:

print `Hello World\n`; # Warning! Wrong kind of quotes!

更新:不,这不是问题所在。如果是这种情况,您将无法使用perl hello.pl运行该程序。这很可能是对shebang线的一些混淆mkHun says。但是,再一次,如果没有看到您的代码,我们就无法帮助您。