perl5的条件语句

perl5的条件语句,perl,Perl,下列哪项不能用于条件语句 而,如果不是,直到,如果不是 或者答案很简单——这些都不能在条件语句块中使用任意代码 if (f()) { while (g()) { h(); } } 您甚至可以使用do在条件表达式中使用任意代码 if (do { my $rv; while (!$rv && f()) { $rv ||= g(); } $rv }) { h(); } 在条件语句块中使用

下列哪项不能用于条件语句

而,如果不是,直到,如果不是


或者答案很简单——这些都不能在条件语句块中使用任意代码

if (f()) {
   while (g()) {
      h();
   }
}
您甚至可以使用
do
在条件表达式中使用任意代码

if (do {
      my $rv;
      while (!$rv && f()) {
         $rv ||= g();
      }
      $rv
}) {
   h();
}

在条件语句块中使用任何类型的语句都没有任何限制,因此答案是所有语句都可以使用

  • 例如:

  • 如果有其他示例:

  • 例如:

  • 如果为elsif-else示例:

  • 这是一个好的开始
    use warnings;
    use strict;
    local $\="\n";
    my $count=10;
    if ($count) {
        while ($count!=0) {
            print $count--; #will print 10, 9, 8, ..., 1
        }
    }
    
    use warnings;
    use strict;
    my $count=10;
    if ($count) {
        if ($count>5) {
            print 'greater than 5';
        }
        else {
            print 'lower or equal to 5';
        }
    }
    
    use warnings;
    use strict;
    local $\="\n";
    my $count=10;
    if ($count) {
        until ($count==0) {
            print $count--; #will print 10, 9, 8, ..., 1
        }
    }
    
    use warnings;
    use strict;
    my $count=10;
    if ($count) {
        if ($count>5) {
            print 'greater than 5';
        }
        elsif ($count==5) {
            print 'equal to 5';
        }
        else {
            print 'lower than 5';
        }
    }