简体   繁体   English

当我运行一个以“#!/ usr / bin / perl -w”开头的文件时,出现错误:“第153行的语法错误,靠近”=〜?“”

[英]When I run a file which is begin with “#!/usr/bin/perl -w”, I get a error: “syntax error at line 153, near ”=~ ?“”

When I run a file which is begin with #!/usr/bin/perl -w , I get a error: 当我运行以#!/usr/bin/perl -w开头的文件时,出现错误:

syntax error at line 153, near "=~ ?" 第153行的语法错误,靠近“=〜?”

I try to add "#!/bin/bash", this error is not append, but I get another 我尝试添加“#!/ bin / bash”,这个错误没有附加,但我得到另一个

error: "line 34: syntax error near unexpected token `('" 错误:“第34行:意外令牌附近的语法错误`('”

line 153 in my file: 我文件中的第153行:

($output_volume =~ ?^([\S]+).mnc?) && ($base_name = $1) ||
  die "sharpen_volume failed: output volume does not appear to be"
  ." a minc volume.\n";

line34 in my file: use MNI::Startup qw(nocputimes); 我文件中的line34:使用MNI :: Startup qw(nocputimes);

$output_volume =~ ?^([\S]+).mnc?

This used to be valid perl and thus might appear in old code and instructional material. 曾经是有效的perl,因此可能出现在旧代码和教学材料中。

From perlop : 来自perlop

In the past, the leading m in m?PATTERN? 在过去,m?PATTERN的领先者? was optional, but omitting it would produce a deprecation warning. 是可选的,但省略它会产生弃用警告。 As of v5.22.0, omitting it produces a syntax error. 从v5.22.0开始,省略它会产生语法错误。 If you encounter this construct in older code, you can just add m. 如果您在旧代码中遇到此构造,则可以添加m。

That is Perl code so the first error message is meaningful. 这是Perl代码,因此第一条错误消息是有意义的。

With delimiters other than // in the match operator you must have the explicit m for it, so 比其他的分隔符//在匹配运算符,你必须有明确的m吧,这样

$output_volume =~ m?^([\S]+).mnc?

It is only with // delimiters that the m may be omitted. 只有//分隔符才能省略m

From Regex Quote-Like Operators (in perlop) 来自正则表达式报价操作员(在perlop中)

If "/" is the delimiter then the initial m is optional. 如果"/"是分隔符,则初始m是可选的。

See perlretut for a tutorial introduction to regex and perlre for reference. 有关regex和perlre的教程介绍,请参阅perlretut以供参考。

Also note that the particular delimiters of ? 还要注意特定的分隔符? trigger specific regex behavior in a special case. 在特殊情况下触发特定的正则表达式行为。 This is discussed by the end of the documentation section in perlop linked above. 这将在perlop链接的perlop文档部分的末尾讨论。

You already have two answers that explain the problem. 您已经有两个解释问题的答案。

  • ? ... ? is no longer valid syntax for a match operator. 不再是匹配运算符的有效语法。 You need m? ... ? 你需要m? ... ? m? ... ? instead. 代替。
  • Until Perl 5.22, your syntax generated a warning. 在Perl 5.22之前,您的语法生成了警告。 Now it's a fatal error (which is what you are seeing). 现在这是一个致命的错误(这是你所看到的)。 So I assume you're now running this on a more recent version of Perl. 所以我假设您现在在更新版本的Perl上运行它。

There are, however, a few other points it is probably worth making. 但是,还有一些其他观点可能值得制作。

  1. You say you tried to investigate this by changing the first line of your file from #!/usr/bin/perl -w to #!/bin/bash . 你说你试图通过将文件的第一行从#!/usr/bin/perl -w更改为#!/bin/bash来调查此问题。 I'm not sure how you think this was going to help. 我不确定你认为这会有什么帮助。 This line defines the program that is used to run your code. 此行定义用于运行代码的程序。 As you have Perl code, you need to run it with Perl. 由于您有Perl代码,您需要使用Perl运行它。 Trying to run it with bash is very unlikely to be useful. 试图用bash运行它是不太可能有用的。
  2. The m? ... ? m? ... ? m? ... ? (or, formerly, ? ... ? ) syntax triggers an obscure and specialised behaviour. (或者,以前, ? ... ? )语法触发了一种模糊和专门的行为。 It seems to me that this behaviour isn't required in your case, so you can probably change it to the more usual / ... / . 在我看来,在您的情况下不需要此行为,因此您可以将其更改为更常见的/ ... /
  3. Your regex contains an unescaped dot character. 你的正则表达式包含一个未转义的点字符。 Given that you seem to be extracting the basename from a filename that has an extension, it seems likely that this should be escaped (using \\. ) so that it matches an actual dot (rather than any character). 鉴于您似乎从具有扩展名的文件名中提取基本名称,似乎应该对其进行转义(使用\\. ),以使其与实际点(而不是任何字符)匹配。
  4. If you are using this code to extract a file's basename, then using a regex probably isn't the best approach. 如果您使用此代码提取文件的基本名称,那么使用正则表达式可能不是最好的方法。 Perhaps take a look at File::Basename instead. 或许可以看一下File :: Basename

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行Perl单行代码时,为什么在意外的标记`(')附近出现“ -bash:语法错误”? - Why do I get “-bash: syntax error near unexpected token `('” when I run my Perl one-liner? 为什么在此Perl代码中出现“'$ rocks ['附近的语法错误””? - Why do I get “Syntax error near '$rocks['” in this Perl code? 如何在使用'/ usr / bin / env perl'运行脚本时找出我正在使用的perl? - how to find out which perl I am using when running a script with '/usr/bin/env perl'? “ =”附近的PERL语法错误 - PERL syntax error near “= ) ” 切换到目录时出错:“perl版本5.12.3无法运行/ usr / bin / shasum” - error when switching to directory: “perl version 5.12.3 can't run /usr/bin/shasum” Perl语法错误 - “语法错误行15附近”}其他“” - Perl Syntax Error - “syntax error line 15 near ”} else“ ” 错误:需要失败的依赖项 /usr/bin/perl - Error: failed dependencies /usr/bin/perl is needed perlist语法错误,位于wordlist.pl第11行,靠近“)$ fh” - perl syntax error at wordlist.pl line 11, near “) $fh” 我是否需要在 Windows 的 Perl 脚本中包含 #?/usr/bin/perl 行? - Do I need to include #!/usr/bin/perl line in Perl script on Windows? 当我使用Email :: MIME :: CreateHTML时,为什么在Email / MIME / CreateHTML.pm的第203行“ croak”附近必须显示正文”附近收到“语法错误”? - Why do I get 'syntax error at Email/MIME/CreateHTML.pm line 203, near “croak ”You must supply a body“”' when I use Email::MIME::CreateHTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM