Perl字符串if/or比较运算符
为什么会出现以下代码:Perl字符串if/or比较运算符,perl,if-statement,operators,conditional-statements,Perl,If Statement,Operators,Conditional Statements,为什么会出现以下代码: # Get new_status print STDERR "Please enter status value (active/inactive): "; ReadMode(1); my $new_status = ReadLine(0); ReadMode(0); print STDERR "\n"; if ( ($new_status ne "active") || ($new_status ne "inactive") ) { die "Status mus
# Get new_status
print STDERR "Please enter status value (active/inactive): ";
ReadMode(1);
my $new_status = ReadLine(0);
ReadMode(0);
print STDERR "\n";
if ( ($new_status ne "active") || ($new_status ne "inactive") )
{
die "Status must be active/inactive.";
}
无论我键入什么,都将始终返回“状态必须为活动/非活动”?(活动、非活动或任何其他,甚至只需按enter键。)
代码似乎有效:
- 我用括号清楚地把这两种说法分开
- 我用绳子 两种情况下的运算符“ne”
- 我使用OR运算符||
谢谢大家!
$new\u status
不能同时等于active
和inactive
,因此条件始终为真。您可能需要&&
而不是|
选择任何看起来更直观的选项:
# option 1
if ( ($new_status ne "active") && ($new_status ne "inactive") )
...
# option 2
unless ( ($new_status eq "active") || ($new_status eq "inactive") )
...
#option 3
my %VALID_STATUS = (
'active' => 1,
'inactive' => 1,
);
if (!$VALID_STATUS{$new_status})
...
让我们来研究一下逻辑
random
)random
既不是“活动”也不是“非活动”,因此您的if
子句变为if(true或true)
——这是真的活动
。第一次检查为false,第二次检查为true,因此得到if(false或true)
-这是true非活动的。这在第一次检查中是正确的,在第二次检查中是错误的,因此您会得到if(true或false)
-这是正确的
语句为假的内容
如果您不想用或
连接两个子句,您应该使用和
(并从这位老程序员那里学习一点技巧——使用和和或而不是&
和|
进行流量控制将不会那么令人困惑。)
更新:总而言之,您的代码中有太多的负面内容,让您感到困惑。在您的回答中,您(无声地!)将if
更改为除非,从而使维护程序员更难理解代码
我会这样写:
my $valid = $new_status eq 'active' || $new_status eq 'inactive';
if (not $valid) {
die "...";
}
use List::Util 'any';
if (not any { $new_status eq $_ } qw[active inactive] ) {
die "...";
}
或者像这样:
my $valid = $new_status eq 'active' || $new_status eq 'inactive';
if (not $valid) {
die "...";
}
use List::Util 'any';
if (not any { $new_status eq $_ } qw[active inactive] ) {
die "...";
}
所以我找到了我问题的答案
问题在于格式
我补充说:
print Dumper $new_status;
在我的代码中,输出为:
$VAR1 = 'active
';
因此,我添加了一个:
chomp $new_status;
现在它工作得很好
$VAR1 = 'active';
最终代码:
# Get new_status
print STDERR "Please enter status value (active/inactive): ";
ReadMode(1);
my $new_status = ReadLine(0);
ReadMode(0);
print STDERR "\n";
chomp $new_status;
unless ( ($new_status eq "active") || ($new_status eq "inactive") )
{
die "Status must be active/inactive.";
}
仅当$new\u status
未处于活动状态
且$new\u status
未处于非活动状态时,才需要显示错误消息,因此
if ( $new_status ne "active" || $new_status ne "inactive" )
应该是
if ( $new_status ne "active" && $new_status ne "inactive" )
我们可以证明这一点。记住
!(A | | B)
相当于!A&&!B
!(A和B)
相当于!A | | |!B
所以
- 如果
$new\u status eq'active'| |$new\u status eq'inactive'
- 如果
,则输入无效!($new_status eq‘active’|$new_status eq‘inactive’)
- 如果
,则输入无效!($new_status eq‘active’)&&!($new_status eq'inactive')
- 如果
$new\u status ne“active”和&$new\u status ne“inactive”,则输入无效。
您需要习惯于看到以下内容:
if ( $new_status ne "active" && $new_status ne "inactive" ) {
die("Status must be active/inactive.\n");
}
但您可能更喜欢使用一种自信的编码风格
$new_status eq "active" || $new_status eq "inactive" # Thing that should be true.
or die("Status must be active/inactive.\n"); # Or what to do when it isn't.
我否决了这个答案,因为你认为我不知道和或或之间的区别,我试图获得一个活动和非活动的状态,但事实并非如此。@Bluz好吧,你实际上使用了|
而不是&&
。。。我的意思是,这没什么大不了的:几乎每个人都发生过。但这个答案是正确的,绝对没有理由对其进行否决。请不要使用,除非用于除琐碎检查以外的任何事情;不得不散布一个隐含的否定让太多人困惑。你用一个除非替换if
,这绝对不是一个细节。事实上,他们做了三个相关的改变:if→除非→均衡器,ne→eq(格式的更改不相关。)请不要建议在流量控制之外使用not
/或/和(或die
,或last
,或return
等)。这会导致错误,比如我在你的回答中不得不修正的错误。@ikegami:该死。你说得对。忽略了我自己的最佳实践。很抱歉。