【发布时间】:2012-12-27 09:50:18
【问题描述】:
我正在尝试使用我被指示为我的"Hello, World!" 程序使用的良好/正确/安全 Perl 代码的以下约定:
use strict;
use warnings;
我已在我的主要 Windows 7 操作系统上使用 (Strawberry) Perl 5.12 创建并成功运行了以下“Hello World”程序:
!#/usr/bin/perl
use strict;
use warnings;
print "Hello, World!\n";
正如预期的那样,我得到的是"Hello, World!"。
让我感到非常奇怪的是,在我的虚拟化 Linux Mint 14 操作系统的终端上运行相同的程序,使用 Perl 5.14,产生了以下错误:
"use" not allowed in expression at /PATH/hello_world.pl line 2, at end of line
syntax error at /PATH/hello_world.pl line 2, near "use strict"
BEGIN not safe after errors--compilation aborted at /PATH/hello_world.pl line 3.
我随后创建了其他“Hello World”程序,没有use strict; 或use warnings; 行,还有一个带有-w,我在一些教程中看到过,如果我没记错的话,这表明警告将被打开。
我的两个替代版本都能正常工作,因为它们产生了我预期的结果:
Hello, World!
我不能确定的是,我是否需要 5.14 及更高版本的 Perl 程序中的 use 语句,或者是否可以在第一行末尾写 -w。
我想我可以在我所有的 Perl 程序中使用一致的标头,可以这么说,无论它们是 Windows 还是 Linux,Perl 5.12 或 5.14 或其他。
【问题讨论】:
-
你把它ftp到你的Linux机器了吗?你用文字模式了吗?
-
"use" 是绝对允许的 :) 问题出在别的地方 - 也许,正如 Edward Thomson 所暗示的,当您将源文件从一台 PC FTP 传输到另一台时,您的源文件已损坏。跨度>
-
没有 FTP。在 Linux Mint 14 中使用 VirtualBox。不使用文本模式。
-
我创建了一个屏幕截图来展示我对不同版本的“Hello, World!”的尝试。程序在这里:link.
标签: perl shebang use-strict