Perl智能匹配错误

时间:2016-01-08 12:10:00

标签: regex linux perl

我的代码中出现了很多Global symbol <symbol> requires explicit package name错误,紧接在我使用智能匹配的代码行之后。在使用智能匹配之前,定义了所有这些全局变量并编写了代码。

if (ref($aActivityErrorStrings) eq "ARRAY" && $sChompedOutput ~~ @$aActivityErrorStrings)

第一行错误给了我一个暗示我使用智能匹配有问题的提示。错误行是

Status message: Failed: syntax error at common.pm line 320, near "$sChompedOutput ~" Global symbol "$rOutput" requires explicit package name

我的Perl版本是5.12

有人可以告诉我智能匹配有什么问题吗?

我的错误。此代码的用户在具有Perl版本5.8的设备上运行它。

感谢您的所有反馈。非常感兴趣。

2 个答案:

答案 0 :(得分:4)

错误:

if (ref($aActivityErrorStrings) eq "ARRAY"
    && $sChompedOutput ~~ @$aActivityErrorStrings))
                                                 ^
                                                 |

你有一个额外的右括号。这证明您的代码可以正常工作:

use strict;
use warnings; 
use 5.020;


my $aActivityErrorStrings = [
    "Error1",
    "Error2",
];

my $sChompedOutput = "Error1";

if (ref($aActivityErrorStrings) eq "ARRAY" 
    && $sChompedOutput ~~ @$aActivityErrorStrings) {
    say 'yes'
}

say "@$aActivityErrorStrings";

--output:--
Smartmatch is experimental at 1.pl line 14.
yes
Error1 Error2

答案 1 :(得分:1)

错误

Global symbol "$sChompedOutput" requires explicit package name at ...
如果您$sChompedOutput未定义,则弹出

以下代码会产生此错误:

my $aActivityErrorStrings = ["mumu", "Bubu", "hello"];
#my $sChompedOutput = "hello";

if (ref($aActivityErrorStrings) eq "ARRAY" && $sChompedOutput ~~ @$aActivityErrorStrings) {
    print "have hello";
}

如果您在#my $sChomped...之前移除评论,则错误就会消失。