【问题标题】:Why is "use" not allowed, as in "use strict;" in Perl 5.14?为什么不允许“使用”,如“使用严格;”在 Perl 5.14 中?
【发布时间】: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


【解决方案1】:

你做了类似的事情

use warnings
use strict;

而不是

use warnings;
use strict;

实际上,我认为这可能是行尾问题。你有 LF,你应该有 CR LF,反之亦然。我已经看到这导致 Perl 认为代码从 shebang 行的中途开始(例如perl use strict;


正如在别处提到的,您发布的代码和您使用的代码是不同的。你真的用过

!use strict;

由于shebang线不好。

!#/u...         # Negation followed by a comment

应该是

#!/u...         # Shebang

【讨论】:

  • 有什么方法可以通过在 shebang 行的末尾添加一个额外的换行符来测试它吗?我可以使用“\n”吗?
  • 我在 shebang 行的末尾用-w"\n" 进行了测试,都没有改变输出/错误。我发现以下document 讨论了如何处理 Windows 和 Linux 之间的 CR/LF 问题。同样,我尝试了警告模式,它建议您实施该模式以防止您刚刚提到的问题发生,但没有任何改变。不过,我不明白为什么在全新的 linux 安装上编写和运行这些基本程序时会遇到 CR LF 问题。我根本没有 FTP 他们。
【解决方案2】:

您的图片显示所有脚本都以!#/usr/bin/perl 开头。这是错误的。这不是一个有效的 she-bang 行,它被读作否定 ! 后跟注释 #。解析将继续并使用 script1.pl perl 将执行! print "Hello world.\n";。这将打印Hello world 并否定print 的结果......不是很有用,但有效的perl。

script2.pl 中 perl 看到 ! use strict;,这是一个编译时错误,因此 perl 失败并报告 use strict; 行的错误。

因此,如果您使用正确的 she-bang 行,所有三个脚本都将按设计工作。

编辑(添加测试脚本):

script1.pl

!#/usr/bin/perl

print "Hello world.\n" ;

致电perl script1.pl 给予

Hello world.

script2.pl

!#/usr/bin/perl

use strict;
use warnings ;

print "Hello world.\n" ;

致电perl script2.pl 给予

"use" not allowed in expression at script2.pl line 3, at end of line
syntax error at script2.pl line 3, near "use strict "
BEGIN not safe after errors--compilation aborted at script2.pl line 4.

使用正确的语法 script3.pl

#!/usr/bin/perl

use strict ;
use warnings ;

print "Hello world.\n" ;

致电perl script3.pl 给予

Hello world.

【讨论】:

  • 为什么投反对票?这就是这里出现问题的原因。我可以验证它。
  • 您的 script1.pl 不会忽略错误的 shebang 行,因为很明显,当您编写 !# 时,它不是 shebang 行。这是!(非)运算符,后跟注释。所以这和写!print 是一样的:反转print 语句的返回值——当然无论如何都会被忽略。在 script2.pl 中,它正在执行 !use,这没有意义,因为 use 是编译时构造,而不是运行时函数。
  • @Smylers 你是对的,我改变了我的答案,给出了失败的正确原因。谢谢。
  • 感谢您的帮助!知道自己犯了一个简单的错误,我感觉好多了!
猜你喜欢
  • 2013-08-06
  • 2011-02-24
  • 2011-08-28
  • 2013-05-15
  • 2012-05-20
  • 2017-03-18
  • 2013-03-14
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多