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运算符||
显然,Perl操作符有些地方我还没有完全掌握。 我做错了什么


谢谢大家!

$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:该死。你说得对。忽略了我自己的最佳实践。很抱歉。