Perl 流程控制
-
if 语句
Perl if 语句由一个布尔表达式和一个或多个语句组成。句法Perl编程语言中的if语句的语法是-if(boolean_expression) { # statement(s) will execute if the given condition is true }
如果布尔表达式的值为true,则将执行if语句中的代码块。如果布尔表达式的计算结果为false,则将执行if语句结束后(右花括号之后)的第一组代码。在布尔上下文中,数字0,字符串'0'和"",空列表()和undef均为false,其他所有值均为true。否定真值!或不返回特殊的false值。流程图
尝试一下$a = 10; # check the boolean condition using if statement if( $a < 20 ) { # if condition is true then print the following printf "a is less than 20\n"; } print "value of a is : $a\n"; $a = ""; # check the boolean condition using if statement if( $a ) { # if condition is true then print the following printf "a has a true value\n"; } print "value of a is : $a\n";
第一个IF语句使用小于运算符(<),该运算符比较两个操作数,如果第一个操作数小于第二个操作数,则返回true,否则返回false。因此,当执行以上代码时,它将产生以下结果-a is less than 20 value of a is : 10 value of a is :
-
if ... else 语句
Perl if 语句后可以跟可选的 else 语句,该语句在布尔表达式为false时执行。句法Perl编程语言中if ... else语句的语法是-if(boolean_expression) { # statement(s) will execute if the given condition is true } else { # statement(s) will execute if the given condition is false }
如果布尔表达式的值为true,则将执行if代码块,否则将执行else代码块。流程图
尝试一下$a = 100; # check the boolean condition using if statement if( $a < 20 ) { # if condition is true then print the following printf "a is less than 20\n"; } else { # if condition is false then print the following printf "a is greater than 20\n"; } print "value of a is : $a\n"; $a = ""; # check the boolean condition using if statement if( $a ) { # if condition is true then print the following printf "a has a true value\n"; } else { # if condition is false then print the following printf "a has a false value\n"; } print "value of a is : $a\n";
执行以上代码后,将产生以下结果-a is greater than 20 value of a is : 100 a has a false value value of a is :
-
if ... elseif ... else 语句
一个 if 语句可以跟着一个可选的elseif ... else语句,这是测试使用单if... elseif声明的各种条件非常有用的。使用if,elsif,else语句时,要牢记几点。- elseif之后可以有一个if可以有零或一个else。
- 一个if可以有零个到多个elsif,并且它们必须位于else之前。
- 一旦elsif成功,将不会测试其余的elsif或else。
句法Perl编程语言中if ... else语句的语法是-if(boolean_expression 1) { # Executes when the boolean expression 1 is true } elsif( boolean_expression 2) { # Executes when the boolean expression 2 is true } elsif( boolean_expression 3) { # Executes when the boolean expression 3 is true } else { # Executes when the none of the above condition is true }
示例:
尝试一下$a = 100; # check the boolean condition using if statement if( $a == 20 ) { # if condition is true then print the following printf "a has a value which is 20\n"; } elsif( $a == 30 ) { # if condition is true then print the following printf "a has a value which is 30\n"; } else { # if none of the above conditions is true printf "a has a value which is $a\n"; }
在这里,我们使用等于运算符 ==,用于检查两个操作数是否相等。如果两个操作数相同,则返回true,否则返回false。执行以上代码后,将产生以下结果-a has a value which is 100
-
unless 语句
unless 语句由布尔表达式和一个或多个语句组成。句法unless(boolean_expression) { # 如果条件为false将执行这里的代码块 }
如果布尔表达式的计算结果为false,则将执行语句中的代码块。如果布尔表达式的计算结果为true,则将执行unless语句结束后(右花括号后)的第一组代码。示例:
尝试一下$a = 20; # check the boolean condition using unless statement unless( $a < 20 ) { # if condition is false then print the following printf "a is not less than 20\n"; } print "value of a is : $a\n"; $a = ""; # check the boolean condition using unless statement unless ( $a ) { # if condition is false then print the following printf "a has a false value\n"; } print "value of a is : $a\n";
第一个,除非语句使用小于运算符(<),该运算符比较两个操作数,如果第一个操作数小于第二个操作数,则返回true,否则返回false。因此,当执行以上代码时,它将产生以下结果-a is not less than 20 value of a is : 20 a has a false value value of a is :
-
unless ... else 语句
unless 语句后可以跟可选的else 语句,该语句在布尔表达式为true时执行。句法unless(boolean_expression) { # boolean_expression 为false 执行 } else { # boolean_expression 为true 执行 }
如果布尔表达式的计算结果为true,则将执行else代码块,否则将执行unless代码块。示例:
尝试一下$a = 100; # check the boolean condition using unless statement unless( $a == 20 ) { # if condition is false then print the following printf "given condition is false\n"; } else { # if condition is true then print the following printf "given condition is true\n"; } print "value of a is : $a\n"; $a = ""; # check the boolean condition using unless statement unless( $a ) { # if condition is false then print the following printf "a has a false value\n"; } else { # if condition is true then print the following printf "a has a true value\n"; } print "value of a is : $a\n";
执行以上代码后,将产生以下结果-given condition is false value of a is : 100 a has a false value value of a is :
-
unless ... elseif ... else 语句
unless 语句可以跟着一个可选的elseif...else语句在使用else,elsif和else语句时,要牢记几点。- unless 可以有零或一个else的。
- 一个unless可以有零到多个的elsif,并且它们必须在else之前。
- 一旦elsif 判断执行,将不会测试其余的elsif或else。
句法unless(boolean_expression 1) { # Executes when the boolean expression 1 is false } elsif( boolean_expression 2) { # Executes when the boolean expression 2 is true } elsif( boolean_expression 3) { # Executes when the boolean expression 3 is true } else { # Executes when the none of the above condition is met }
示例:
尝试一下$a = 20; # check the boolean condition using if statement unless( $a == 30 ) { # if condition is false then print the following printf "a has a value which is not 20\n"; } elsif( $a == 30 ) { # if condition is true then print the following printf "a has a value which is 30\n"; } else { # if none of the above conditions is met printf "a has a value which is $a\n"; }
在这里,我们使用等于运算符==,用于检查两个操作数是否相等。如果两个操作数相同,则返回true,否则重新运行为false。执行以上代码后,将产生以下结果-a has a value which is not 20
-
switch 语句
switch 语句允许一个变量来针对值的列表进行测试。并针对每个case 检查 switch ,switch 模块已经实现使用Filter::Util::Call和Text::Balanced,需要安装这两个模块。句法use Switch; switch(argument) { case 1 { print "number 1" } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (\@array) { print "number in list" } case /\w+/ { print "pattern" } case qr/\w+/ { print "pattern" } case (\%hash) { print "entry in hash" } case (\&sub) { print "arg to subroutine" } else { print "previous case not true" } }
以下规则适用于switch语句-- switch 语句采用任何类型的单个标量参数,在括号中指定。
- 该值后跟一个块,该块可以包含一个或多个case语句,后跟一个Perl语句块。
- case 语句采用单个标量参数,并在case参数和当前switch 值之间选择合适的匹配类型。
- 如果匹配成功,则执行与case语句关联的块。
- 一个switch语句可以有一个可选的 else ,必须出现在switch 的结束。当没case 匹配时,怎执行该块。
- 如果case块执行了无目标的 next,则控制权立即转移到case语句之后的语句(即通常是另一个case)。
- 并非每个case 都需要包含next。如果没有 next 出现,则控制流不会落入到下一个case 和 else 的情况。
示例:use Switch; $var = 10; @array = (10, 20, 30); %hash = ('key1' => 10, 'key2' => 20); switch($var) { case 10 { print "number 100\n" } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (\@array) { print "number in list" } case (\%hash) { print "entry in hash" } else { print "previous case not true" } }
执行以上代码后,将产生以下结果--number 100
在switch语句中,though-though通常不是一个好主意。但是,现在考虑一个失败的情况,我们将使用next将控件转移到下一个匹配的情况,在这种情况下为列表-use Switch; $var = 10; @array = (10, 20, 30); %hash = ('key1' => 10, 'key2' => 20); switch($var) { case 10 { print "number 100\n"; next; } case "a" { print "string a" } case [1..10,42] { print "number in list" } case (\@array) { print "number in list" } case (\%hash) { print "entry in hash" } else { print "previous case not true" } }
执行以上代码后,将产生以下结果-number 100 number in list
-
?: 运算符
条件运算符 ?:可用于替换if ... else语句。它具有以下一般形式-Exp1 ? Exp2 : Exp3;
其中Exp1,Exp2和Exp3是表达式。注意冒号的使用和放置。 值 表达式是这样确定的:Exp1被求值,将计算Exp2如果为true成为整个表达式的值。 如果Exp1为false,则计算Exp3,其值成为表达式的值。以下是使用此运算符的简单示例-示例:
尝试一下$name = "Mo"; $age = 10; $status = ($age > 60 )? "A senior citizen" : "Not a senior citizen"; print "$name is - $status\n";
将产生以下结果-Mo is - Not a senior citizen