2016-08-03 50 views
-3

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

我可以用perl hello.pl执行它,但是当我尝试./hello.pl,一个错误来了:错误:没有这样的文件的“Hello World”

的原因是什么?

编辑:

我的程序

use warnings; 
use strict; 
use 5.010; 

print "Hello World"; 

我的错误:

./hello.pl: line 1: use: command not found ./hello.pl: line 2: use: command not found ./hello.pl: line 3: use: command not found Error: no such file "Hello World"

+6

请发布代码。从错误信息 - 我猜你已经有一些反应,或者在其他方面做你的脚本奇怪的事情。 – Sobrique

+1

应该重复[为什么shebang行总是第一行?](http://stackoverflow.com/questions/12910744)...我之前关闭了错误的重复。 –

+1

你说它通过告诉perl来运行它并且它不能自行运行,应该告诉你,由它本身调用,perl没有运行它。你指定你给它的扩展名为“.pl”,并且期望它在perl下运行,告诉我你正在考虑扩展很重要的win32行。但是,它们对于unix/linux无关紧要。这就是为什么你需要“shbang line”来告诉shell应该执行什么程序。 – Axeman

回答

7

使用shebang line在程序的顶部,应该把use warnings;use strict;它告诉给内核或Apache解释为一个Perl脚本。

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

print "hello world"; 

,更多的是家当

Does the shebang determine the shell which runs the script?

Why should the shebang line always be the first line?

+0

我建议这也需要'使用严格;使用警告;' - 早日习惯这种习惯是很好的!:) – Sobrique

+1

@Sobrique谢谢:)文章编辑 – mkHun

+0

它的工作,非常感谢! – DanielJames

1

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

你的程序应该是这样的:

#!/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"; 

我的第一个版本将使用字符串周围单引号和我的第二个版本使用双引号字符。如果您收到“没有这样的文件”错误,那么您可能使用反引号 - 用于执行外部程序。请问您print线是这样的:

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

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