在其他語言中我會寫爲什麼「x = a或b」在Perl中不起作用?
testvar = onecondition OR anothercondition;
有,如果這兩個條件中的testvar是真實的。但在Perl中,這不能按預期工作。
我想檢查內容變量爲空或與特定正則表達式匹配的情況。我有這個示例程序:
my $contents = "abcdefg\n";
my $criticalRegEx1 = qr/bcd/;
my $cond1 = ($contents eq "");
my $cond2 = ($contents =~ $criticalRegEx1);
my $res = $cond1 or $cond2;
if($res) {print "One or the other is true.\n";}
我本來希望$ res包含「1」,或者在用if()進行測試時證明是真的。但它包含空字符串。
我該如何在Perl中實現這個功能?周圍表達
檢查出[運算符優先級表(HTTP: //perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity)。用'||'比較'或'。 – user2864740 2014-09-01 10:02:42