Perl如何使用given语句?代码示例

2021年4月1日18:04:05 发表评论 984 次浏览

given-when语句,Perl代替了将变量与多个整数值进行比较的长if语句。

  • given-when语句是多向分支语句。它提供了一种简单的方法, 可以根据表达式的值将执行分派到代码的不同部分。
  • given是允许值更改执行控制的控制语句。

given-when在Perl中类似于开关盒ofC / C ++, pythonor的PHP。就像开关语句, 它还会用不同的情况替换多个if语句。

语法如下:

given(expression)
{
    when(value1) {Statement;}
    when(value2) {Statement;}

    default {# Code if no other case matches}
}

given-when语句还使用了另外两个关键字,即break和continue。这些关键字维护程序流,并帮助退出程序执行或在特定值处跳过执行。

break: break关键字用于退出when块。在Perl中,没有必要在每个when块之后显式地写断点。它已经隐式定义了。

continue:另一方面,如果block是正确的,continue会移动到下一个block。

Perl如何使用given语句?代码示例1

在一个given-when语句, 条件语句不能重复多次什么时候语句, 这是因为Perl仅检查该条件的第一次出现, 而下一个重复的语句将被忽略。此外, 必须在所有什么时候语句, 因为编译器会检查是否与每个条件匹配什么时候声明顺序, 以及是否放置default在这两者之间, 则需要一个打破在那边, 将打印默认语句。

例子:

#!/usr/bin/perl
  
# Perl program to print respective day
# for the day-code using given-when statement
use 5.010;
  
# Asking the user to provide day-code
print "Enter a day-code between 0-6\n" ;  
  
# Removing newline using chomp
chomp ( my $day_code = <>);  
  
# Using given-when statement
given ( $day_code ) 
{  
     when ( '0' ) { print 'Sunday' ;}  
     when ( '1' ) { print 'Monday' ;}  
     when ( '2' ) { print 'Tuesday' ;}  
     when ( '3' ) { print 'Wednesday' ;} 
     when ( '4' ) { print 'Thursday' ;}  
     when ( '5' ) { print 'Friday' ;} 
     when ( '6' ) { print 'Saturday' ;}
     default { print 'Invalid day-code' ;}  
}

输入如下:

4

输出如下:

Enter a day-code between 0-6
Thursday

嵌套given-when声明

嵌套given-when语句是指given-when另一个内部的语句given-when声明。这可以用于维护用户为特定输出集提供的输入层次结构。

语法如下:

given(expression)
{
    when(value1) {Statement;}
    when(value2) {given(expression)
                  {
                     when(value3) {Statement;}   
                     when(value4) {Statement;}   
                     when(value5) {Statement;} 
                     default{# Code if no other case matches}
                  }
                 }
    when(value6) {Statement;}

    default {# Code if no other case matches}
}

以下是嵌套的示例given-when声明:

#!/usr/bin/perl
  
# Perl program to print respective day
# for the day-code using given-when statement
use 5.010;
  
# Asking the user to provide day-code
print "Enter a day-code between 0-6\n" ;  
  
# Removing newline using chomp
chomp ( my $day_code = <>);  
  
# Using given-when statement
given ( $day_code ) 
{  
     when ( '0' ) { print 'Sunday' ;}  
     when ( '1' ) { print "What time of day is it?\n" ;
                  chomp ( my $day_time = <>);
                   
                  # Nested given-when statement
                  given ( $day_time )
                  {
                      when ( 'Morning' ) { print 'It is Monday Morning' };
                      when ( 'Noon' )    { print 'It is Monday noon' };
                      when ( 'Evening' ) { print 'It is Monday Evening' };
                      default { print 'Invalid Input' };
                  }
                 }  
     when ( '2' ) { print 'Tuesday' ;}  
     when ( '3' ) { print 'Wednesday' ;} 
     when ( '4' ) { print 'Thursday' ;}  
     when ( '5' ) { print 'Friday' ;} 
     when ( '6' ) { print 'Saturday' ;}
     default { print 'Invalid day-code' ;}  
}

输入如下:

1
Morning

输出如下:

Enter a day-code between 0-6
What time of day is it?
It is Monday Morning

输入如下:

3

输出如下:

Enter a day-code between 0-6
Wednesday

在上述示例中, 当输入日代码为1以外的任何值时, 该代码将不会进入嵌套given-when块, 输出将与前面的示例相同, 但是如果我们给定1作为输入, 它将执行嵌套given-when块, 输出将与前面的示例不同。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: