我有下面的代码在其中检查排除的数组中的特定变量位置,它与除一个(abc / def / libraries / linux_3.2.60-1 + deb7u3.dsc)以外的所有数组元素都能正常工作。当我将此元素提供为我的位置时,即使其被排除,它也会打印“位置不排除”。
如何制作我的代码以获取和排除此元素?
use strict;
use warnings;
my @excluded = (
"xyz/efg/headers/",
"abc/def/libraries/jni-mr.h",
"abc/def/libraries/linux_3.2.60-1+deb7u3.dsc",
);
my $location = "abc/def/libraries/linux_3.2.60-1+deb7u3.dsc";
my $badpath = 0;
foreach (@excluded) {
# -- Check if location is contained in excluded array
if ($location =~ /^$_/) {
$badpath = 1;
print "location is excluded : $location \n";
}
}
if (! $badpath) {
print "location is not excluded : $location \n";
}
所需输出:
location is excluded : abc/def/libraries/linux_3.2.60-1+deb7u3.dsc
电流输出:
location is not excluded : abc/def/libraries/linux_3.2.60-1+deb7u3.dsc
使用quotemeta($text)
或\Q$text\E
(在双引号或正则表达式文字内)创建与的值匹配的模式$text
。换句话说,使用
if ($location =~ /^\Q$_\E/)
代替:
if ($location =~ /^$_/)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句