Perl打破了If语句 [英] Perl Breaking out of an If statement

查看:100
本文介绍了Perl打破了If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题刚刚出现:如果语句如何突破?我有一个很长的if语句,但有一种情况我可以在早期就突破它。



在循环中我可以这样做:

  while(something){
last if some_condition;
blah,blah,blah
...
}

但是,我可以用if语句做同样的事吗?

  if(some_condition){
blah,blah,blah如果$ some_other_condition,
最后; #无需继续...
blah,blah,blah
...
}

我知道我可以将 if 语句放在一个块中,然后我可以突破该块:

 } 
if(some_condition){
...
last if some_other_condition; #无需继续...
blah,blah,blah
...
}
}

或者,我可以创建一个子程序(可能以编程方式更好):

  if(some_condition){
run_subroutine();
}

sub run_subroutine {
blah,blah,blah
如果$ some_other_condition则返回;
blah,blah,blah
...
}

但是有没有退出if条件?






分辨率



<问题出现了,因为我正在用他们的代码帮助某人。在一个相当长的 if 语句中,还有其他几个 if 语句。代码看起来像这样:

  if($ condition1){
blah,blah,blah;
if(not $ condition2){
blah,blah,blah;
if(not $ condition3){
blah,blah,blah;
}
}
}

我认为整件事情可能通过这样做可以提高可读性:

  if($ condition1){
last if condition2;
blah,blah,blah;
最后如果$ condition3;
blah,blah,blah;
}

这表明的正常流量如果语句是标准语句,但在某些条件下, if 语句提前退出 - 就像使用 last 下一个中,而用于循环退出循环。



我喜欢 mpapec的使用解决方案标签 - 即使我不使用标签本身。标签是我的的描述,如果

  IF-UNDER-条件1:
{
if($ condition1){
last if $ condition2;
blah,blah,blah;
最后如果$ condition3;
blah,blah,blah;
}
}

虽然它不是标准的编码技术,代码流很明显,一个典型的低级Perl开发人员(我离开后必须维护这段代码的开发人员)可以弄清楚代码在做什么并维护它。他们甚至可以在这个过程中学到一些东西。

解决方案

你可以使用基本块 last next 的约束并且重做,因此可能会中断。

  if( $ condition){EXIT_IF:{

last EXIT_IF; #从代码块中断

printnever get's execution\\\
;
}}






  EXIT_IF:{
if($ condition){

last EXIT_IF; #从代码块中断

printnever get's execution\\\
;
}
}


This one just came up: How do I break out of an if statement? I have a long if statement, but there is one situation where I can break out of it early on.

In a loop I can do this:

while (something ) {
    last if $some_condition;
    blah, blah, blah
    ...
}

However, can I do the same with an if statement?

if ( some_condition ) {
    blah, blah, blah
    last if $some_other_condition; # No need to continue...
    blah, blah, blah
    ...
}

I know I could put the if statement inside a block, and then I can break out of the block:

}
    if ( some_condition ) {
        ...
        last if $some_other_condition; # No need to continue...
        blah, blah, blah
        ...
    }
}

Or, I can create a subroutine (which is probably better programmatically):

if ( some_condition ) {
    run_subroutine();
}

sub run_subroutine {
    blah, blah, blah
    return if $some_other_condition;
    blah, blah, blah
    ...
}

But is there anyway to exit an if condition?


Resolution

The question came up because I was helping someone with their code. Inside a fairly long if statement, there were several other if statements embedded in it. The code looked something like this:

 if ( $condition1 ) {
    blah, blah, blah;
    if ( not $condition2 ) {
       blah, blah, blah;
       if ( not $condition3 ) {
          blah, blah, blah;
       }
    }
}

I thought the whole thing could be made more readable by doing this:

if ( $condition1 ) {
    last if $condition2;
    blah, blah, blah;
    last if $condition3;
    blah, blah, blah;
}

This shows that the normal flow of the if statement is standard, but under certain conditions, the if statement was exited early -- much like using last or next in a while or for loop to exit the loop.

I liked mpapec's solution of using a label -- even if I don't use the label itself. The label is a description of my if:

IF-UNDER-CONDITION1:
{
    if ( $condition1 ) {
        last if $condition2;
        blah, blah, blah;
        last if $condition3;
        blah, blah, blah;
    }
}

Although it isn't a standard coding technique, the flow of the code is obvious enough that a typical low-level Perl developer (the one that has to maintain this code after I leave) could figure out what the code is doing and maintain it. They may even learn something in the process.

解决方案

You can use basic block which is subject to last, next and redo, so there is possible break from it.

if ($condition) {EXIT_IF:{

   last EXIT_IF; # break from code block

   print "never get's executed\n";
}}


EXIT_IF: {
  if ($condition) {

     last EXIT_IF; # break from code block

     print "never get's executed\n";
  }
}

这篇关于Perl打破了If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆