Perl equivalent of (Postgre)SQL BETWEEN operator?

前端 未结 5 731
囚心锁ツ
囚心锁ツ 2021-01-22 07:17

Is there such a thing? The equivalent of a <= expr <= b, or in SQL parlance expr BETWEEN a AND b, where expr is evaluated only once

5条回答
  •  执笔经年
    2021-01-22 08:02

    There are a variety of ways to do that in Perl.

    if( $a < $x and $x < $b ){ ... }
    ... if $a < $x and $x < $b;
    
    use 5.10.1;
    if( $x ~~ [$a..$b] ){ ... }
    
    given( $x ){
      when( [$a..$b] ){ ... }
    }
    
    use 5.11.0; # development branch
    given( $x ){
      ... when [$a..$b];
    }
    

提交回复
热议问题