Perl学习笔记(3)-条件与循环语句以及错误处理

(0)前言

很多程序语言其实是比较类似的。基本上都有数据类型,循环结构,条件语句等内容。Perl也不例外。

(1)if语句

if语句由一个布尔表达式后跟一个或多个语句组成。语法格式如下所示:

1
2
3
4
if(boolean_expression){
表达式
# 在布尔表达式 boolean_expression 为 true 执行
}

如果布尔表达式 boolean_expression为 true,则 if 语句内的代码块将被执行。如果布尔表达式为 false,则 if 语句结束后的第一组代码(闭括号后)将被执行,如下所示:

if语句案例第一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
biotest@ubuntu:~/perl/03condition$ cat if01.pl
#!/usr/bin/perl
$a=10;
if($a<20){
print "a is less 20\n";
}
print "The value of a is: $a\n";
$a="";
if($a){
print "variable of a is true.\n";
}
print "The value of a is $a\n";
biotest@ubuntu:~/perl/03condition$ perl if01.pl
a is less 20
The value of a is: 10
The value of a is

(2)if…else语句

一个if语句后可跟一个可选的else语句,else语句在布尔表达式为false时执行。语法语法格式如下所示:

1
2
3
4
5
6
7
if(boolean_expression){
表达式A;
# 在布尔表达式 boolean_expression 为 true 执行
}else{
表达式B;
# 在布尔表达式 boolean_expression 为 false 执行
}

如果布尔表达式boolean_expression为true,则执行if块内的表达式A。如果布尔表达式为false,则执行else块内的表达式B,如下所示:

if…else语句案例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
biotest@ubuntu:~/perl/03condition$ cat ifelse01.pl
#!/usr/bin/perl
$a = 100;
if($a<20){
print "a is less 20\n";
}else{
print "a is more 20\n";
}
print "The value of a is $a\n";
$a = "";
if($a){
print "condition of a is true.\n";
}else{
print "condition of a ls flase.\n";
}
print "The value of a is $a\n";
biotest@ubuntu:~/perl/03condition$ perl ifelse01.pl
a is more 20
The value of a is 100
condition of a ls flase.
The value of a is

三元运算符 ?:

可以使用 条件运算 ? :来简化if...else语句的操作。通常格式为:Exp1 ? Exp2 : Exp3;如果 Exp1 表达式为 true ,则返回 Exp2 表达式计算结果,否则返回 Exp3,如下所示:

1
2
3
4
5
6
7
8
9
biotest@ubuntu:~/perl/03condition$ cat ifelse02.pl
#!/usr/bin/perl
$a = 100;
$result=($a<20)?"a is less 20\n":"a is more 20\n";
print "$result\n";
biotest@ubuntu:~/perl/03condition$ perl ifelse02.pl
a is more 20

(3)if…elsif…else语句

一个if语句后可跟一个可选的elsif语句,然后再跟另一个else语句,这种条件判断语句在多个条件的情况下非常有用,在使用if,elsif,else语句时你需要注意以下几点:

  1. if语句后可以跟上0个或1个else语句,但是elsif后面必须有else语句。
  2. if语句后可以跟上0个或1个elsif语句,但它们必须写在else语句前。
  3. 如果其中的一个elsif执行成功,其他的elsif和else将不再被执行。

语法格式如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(boolean_expression1){
  表达式A
#在布尔表达式boolean_expression1为true执行A
}
elsif(boolean_expression2){
  表达式B
  #在布尔表达式boolean_expression2为true执行B
}
elsif(boolean_expression3){
  表达式C
  #在布尔表达式boolean_expression3为true执行C
}
else{
  表达式D
  #布尔表达式的条件都为false时执行D
}

if…elsif案例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/perl/03condition$ cat ifelsif.pl
#!/usr/bin/perl
$a=100;
if($a==20){
print "The value of a is 20\n";
}elsif($a==30){
print "The value of a is 30\n";
}else{
print "The value of a is $a\n";
}
biotest@ubuntu:~/perl/03condition$ perl ifelsif.pl
The value of a is 100

(4)unless语句

一个unless语句由一个布尔表达式后跟一个或多个语句组成。语法格式如下所示:

1
2
3
4
unless(boolean_expression){
表达式A
#在布尔表达式boolean_expression为false执行A
}

unless使用案例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
biotest@ubuntu:~/perl/03condition$ cat unless.pl
#!/usr/bin/perl
$a=120;
unless($a<20){
print "a is more or equeal 20\n";
}
print "The value of a is $a\n";
$a="";
unless($a){
print "The condition of a is false\n";
}
print "The value of a is $a\n";
biotest@ubuntu:~/perl/03condition$ perl unless.pl
a is more or equeal 20
The value of a is 120
The condition of a is false
The value of a is

(5)unless…else语句

一个unless语句后可跟一个可选的else语句,else语句在布尔表达式为true时执行。语法格式如下所示:

1
2
3
4
5
6
7
unless(boolean_expression){
表达式A
#在布尔表达式boolean_expression为false执行A
}else{
表达式B
#在布尔表达式boolean_expression为true执行B
}

如果布尔表达式boolean_expression为false,则执行unless块内的代码。如果布尔表达式为true,则执行else块内的代码。

unless…else使用案例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
biotest@ubuntu:~/perl/03condition$ cat unlessif.pl
#!/usr/bin/perl
$a=100;
unless($a==20){
print "The condition given is false\n";
}else{
print "The conditoin given is true\n";
}
print "The value of a is $a\n";
$a="";
unless($a){
print "The condition of a given is false\n";
}else{
print "The conditoin of a given is true\n";
}
print "THe value of a is $a\n";
biotest@ubuntu:~/perl/03condition$ perl unlessif.pl
The condition given is false
The value of a is 100
The condition of a given is false
THe value of a is

(6)unless…elsif…else statement

一个unless语句后可跟一个可选的elsif语句,然后再跟另一个else语句,这种条件判断语句在多个条件的情况下非常有用。
在使用unless,elsif,else语句时你需要注意以下几点。

  1. unless语句后可以跟上0个或1个else语句,但是elsif后面必须有else语句。
  2. unless语句后可以跟上0个或1个elsif语句,但它们必须写在else语句前。
  3. 如果其中的一个elsif执行成功,其他的elsif和else将不再被执行。

它的使用格式如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
unless(boolean_expression1){
表达式A
#在布尔表达式boolean_expression1为false执行A
}
elsif(boolean_expression2){
表达式B
#在布尔表达式boolean_expression2为true执行B
}
elsif(boolean_expression3){
表达式C
#在布尔表达式boolean_expression3为true执行C
}
else{
表达式A
#没有条件匹配时执行D
}

unless…elsif…else statement使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/perl/03condition$ cat unless03.pl
#!/usr/bin/perl
$a=20;
unless($a==30){
print "The vlaue of a is not 30\n";
}elsif($a==30){
print "The value of a is 30\n";
}else{
print "The value of a is $a\n";
}
biotest@ubuntu:~/perl/03condition$ perl unless03.pl
The vlaue of a is not 30

(7)switch

一个switch语句允许测试一个变量等于多个值时的情况。每个值称为一个case,且被测试的变量会对每个switch case进行检查。
switch case执行是基于Switch模块,Switch模块使用Filter::Util::CallText::Balanced来执行,这两个模块都需要安装。

switch的格式如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
use Switch;
switch(argument){
case 1 { print "数字 1" }
case "a" { print "字符串 a" }
case [1..10,42] { print "数字在列表中" }
case (\@array) { print "数字在数组中" }
case /\w+/ { print "正则匹配模式" }
case qr/\w+/ { print "正则匹配模式" }
case (\%hash) { print "哈希" }
case (\&sub) { print "子进程" }
else { print "不匹配之前的条件" }
}

以下是switch语句的规则:

  1. switch语句的括号中可以使用任意类型的标量参数。
  2. 在一个switch中可以有任意数量的case语句。每个case后跟一个要比较的值和一个冒号。
  3. case语句后的标量会与switch语句的标量进行比较,判断是否相等。
  4. 当被测试的变量等于case中的常量时,case后跟的语句将被执行,直到遇到break语句为止。
  5. switch语句可以有一个可选的else,该语句在最后面,该语句在所有case不匹配的情况下执行。
  6. 当匹配case后,会执行case语句块代码,执行后跳出switch语句。
  7. 当匹配case后,如果我们需要继续执行接下来的case语句,则需要添加next语句。

switch使用案例

代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
biotest@ubuntu:~/perl/03condition$ cat switch01.pl
#!/usr/bin/perl
use Switch;
$var=10;
@array=(10,20,30);
%hash=('key1'=>10,'key2'=>20);
switch($var){
case 10 {print "Number 10\n";next;}
case "a" {print "string a "}
case [1..10,42] {print "Number in list"}
case (\@array) {print "Number in array"}
case (\%hash) {print "In hash"}
else {print "There is no matching condition"}
}
biotest@ubuntu:~/perl/03condition$ perl switch01.pl
Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /home/biotest/miniconda2/lib/perl5/site_perl/5.22.0/x86_64-linux-thread-multi /home/biotest/miniconda2/lib/perl5/site_perl/5.22.0 /home/biotest/miniconda2/lib/perl5/5.22.0/x86_64-linux-thread-multi /home/biotest/miniconda2/lib/perl5/5.22.0 .) at switch01.pl line 3.
BEGIN failed--compilation aborted at switch01.pl line 3.

从结果来看,这是因为Perl没有安装Switch模块,Filter::Util::CallText::Balanced接着需要安装这个模块,代码如下所示:

1
2
3
4
perl -MCPAN -e shell #进入cpan
install Switch
install Filter::Util::Call
install Text::Balanced

安装后,继续运行,如下所示:

1
2
3
biotest@ubuntu:~/perl/03condition$ perl switch01.pl
Number 10
Number in list

(8)while循环

有的时候,我们可能需要多次执行同一块代码。一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推。编程语言提供了更为复杂执行路径的多种控制结构。Perl中有while循环,until循环,for循环。
while语句在给定条件为true时,重复执行语句或语句组,循环主体执行之前会先测试条件。
while循环的语法格式如下所示:

1
2
3
4
while(condition)
{
 statement(s);
}

在这里,statement(s)可以是一个单独的语句,也可以是几个语句组成的代码块。condition可以是任意的表达式,当为任意非零值时都为true。当条件为true时执行循环。当条件为false时,程序流将继续执行紧接着循环的下一条语句。

while循环案例使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
biotest@ubuntu:~/perl/03condition$ cat while.pl
#!/usr/bin/perl
$a=10;
#execute while loop
while($a<20){
printf "The value of a is: $a\n";
$a=$a+1;
}
biotest@ubuntu:~/perl/03condition$ perl while.pl
The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14
The value of a is: 15
The value of a is: 16
The value of a is: 17
The value of a is: 18
The value of a is: 19

程序中在变量a小于20时执行循环体,在变量a大于等于20时,退出循环。

(8)until循环

until语句在给定条件为false时,重复执行语句或语句组。语法格式如下所示:

1
2
3
4
until(condition)
{
 statement(s);
}

在这里,statement(s)可以是一个单独的语句,也可以是几个语句组成的代码块。condition可以是任意的表达式,当条件为false时执行循环。当条件为true时,程序流将继续执行紧接着循环的下一条语句。

until循环案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
biotest@ubuntu:~/perl/03condition$ cat until.pl
#!/usr/bin/perl
$a=5;
until($a>10){
printf "The value of a is: $a\n";
$a=$a+1;
}
biotest@ubuntu:~/perl/03condition$ perl until.pl
The value of a is: 5
The value of a is: 6
The value of a is: 7
The value of a is: 8
The value of a is: 9
The value of a is: 10

程序中在变量$a小于10时执行循环体,在变量$a大于等于10时,退出循环。

(9)for循环

for循环用于多次执行一个语句序列,简化管理循环变量的代码。语法格式如下所示:

1
2
3
for(init;condition;increment){
 statement(s);
}

下面是for循环的控制流程解析:

  1. init会首先被执行,且只会执行一次。这一步允许您声明并初始化任何循环控制变量。您也可以不在这里写任何语句,只要有一个分号出现即可。
  2. 接下来,会判断condition。如果为true,则执行循环主体。如果为false,则不执行循环主体,且控制流会跳转到紧接着for循环的下一条语句。
    在执行完for循环主体后,控制流会跳回上面的increment语句。该语句允许您更新循环控制变量。该语句可以留空,只要在条件后有一个分号出现即可。
  3. 条件再次被判断。如果为true,则执行循环,这个过程会不断重复(循环主体,然后增加步值,再然后重新判断条件)。在条件变为false时,for循环终止。
  4. 在这里,statement(s)可以是一个单独的语句,也可以是几个语句组成的代码块。condition可以是任意的表达式,当条件为false时执行循环。
    当条件为true时,程序流将继续执行紧接着循环的下一条语句。

for循环案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
biotest@ubuntu:~/perl/03condition$ cat for.pl
#!/usr/bin/perl
for($a=1;$a<10;$a=$a+1){
print "The value of a is: $a\n";
}
biotest@ubuntu:~/perl/03condition$ perl for.pl
The value of a is: 1
The value of a is: 2
The value of a is: 3
The value of a is: 4
The value of a is: 5
The value of a is: 6
The value of a is: 7
The value of a is: 8
The value of a is: 9

(10)foreach循环

foreach循环用于迭代一个列表或集合变量的值。语法格式如下所示:

1
2
3
foreach var (list) {
...
}

foreach循环案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/perl/03condition$ cat foreach.pl
#!/usr/bin/perl
@list=(2,12,36,42,51);
foreach $a(@list){
print "The value of a : $a\n";
}
biotest@ubuntu:~/perl/03condition$ perl foreach.pl
The value of a : 2
The value of a : 12
The value of a : 36
The value of a : 42
The value of a : 51

(11)do…while循环

do…while循环是在循环的尾部检查它的条件。do…while循环与while循环类似,但是do…while循环会确保至少执行一次循环。语法格式如下所示:

1
2
3
4
do
{
 statement(s);
}while(condition);

请注意,条件表达式出现在循环的尾部,所以循环中的statement(s)会在条件被测试之前至少执行一次。
如果条件为true,控制流会跳转回上面的do,然后重新执行循环中的statement(s)。这个过程会不断重复,直到给定条件变为false为止。

do…while案例使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
biotest@ubuntu:~/perl/03condition$ cat dowhile.pl
#!/usr/bin/perl
$a=10;
do{
printf "The value of a is: $a\n";
$a=$a+1;
}while($a<15);
biotest@ubuntu:~/perl/03condition$ perl dowhile.pl
The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14

(12)循环嵌套

Perl语言允许在一个循环内使用另一个循环,下面演示几个实例来说明这个概念。

(12.1)嵌套for循环语句的语法

格式如下所示:

1
2
3
4
5
6
for ( init; condition; increment ){
for ( init; condition; increment ){
statement(s);
}
statement(s);
}

(12.2)while循环嵌套

使用格式如下所示:

1
2
3
4
5
6
while(condition){
while(condition){
statement(s);
}
statement(s);
}

使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
biotest@ubuntu:~/perl/03condition$ cat while2.pl
#!/usr/bin/perl
$a=0;
$b=0;;
while($a<3){
$b=0;
while($b<3){
print "a=$a,b=$b\n";
$b=$b+1;
}
$a=$a+1;
print "a=$a\n\n";
}
biotest@ubuntu:~/perl/03condition$ perl while2.pl
a=0,b=0
a=0,b=1
a=0,b=2
a=1
a=1,b=0
a=1,b=1
a=1,b=2
a=2
a=2,b=0
a=2,b=1
a=2,b=2
a=3

(13.3)do…while循环嵌套

格式如下所示:

1
2
3
4
5
6
7
do{
statement(s);
do{
statement(s);
}while( condition );
}while( condition );

(13.4)until循环嵌套

格式如下所示:

1
2
3
4
5
6
until(condition){
until(condition){
statement(s);
}
statement(s);
}

(13.5)foreach循环嵌套

格式如下:

1
2
3
4
5
6
foreach $a (@listA){
foreach $b (@listB){
statement(s);
}
statement(s);
}

(13)循环控制语句

循环控制语句改变了代码的执行顺序,通过它你可以实现代码的跳转,Perl中的循环控制语句有这些:

控制语句 描述
next 语句 停止执行从next语句的下一语句开始到循环体结束标识符之间的语句,转去执行continue语句块,然后再返回到循环体的起始处开始执行下一次循环。
last 语句 退出循环语句块,从而结束循环
continue 语句 continue 语句块通常在条件语句再次判断前执行。
redo 语句 redo 语句直接转到循环体的第一行开始重复执行本次循环,redo语句之后的语句不再执行,continue语句块也不再执行;
goto 语句 Perl 有三种 goto 形式:got LABLE,goto EXPR,和 goto &NAME。

(13.1) next语句

next语句用于停止执行从next语句的下一语句开始到循环体结束标识符之间的语句,转去执行continue语句块,然后再返回到循环体的起始处开始执行下一次循环。语法格式如下所示:

1
next[LABEL];

其中LABEL是可选的,如果没有指定LABEL,next语句将返回到循环体的起始处开始执行下一次循环,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
biotest@ubuntu:~/perl/03condition$ cat next.pl
#!/usr/bin/perl
$a=10;
while($a<20){
if($a==15)
{
# jump loop
$a=$a+1;
next;
}
print "The value of a is: $a\n";
$a = $a+1;
}
biotest@ubuntu:~/perl/03condition$ perl next.pl
The value of a is: 10
The value of a is: 11
The value of a is: 12
The value of a is: 13
The value of a is: 14
The value of a is: 16
The value of a is: 17
The value of a is: 18
The value of a is: 19

从结果可以看出,如果a等于15了,就跳出这个循环。

(13.2) last语句

last 语句用于退出循环语句块,从而结束循环,last语句之后的语句不再执行,continue语句块也不再执行。语法格式如下所示:

1
last [LABEL];

案例如下所示:

1
2

(13.3)continue

continue块通常在条件语句再次判断前执行。continue语句可用在while和foreach循环中。
while循环中continue语句语法格式如下所示:

1
2
3
4
5
while(condition){
statement(s);
}continue{
statement(s);
}

foreach循环中continue语句语法格式如下所示:

1
2
3
4
5
foreach $a (@listA){
statement(s);
}continue{
statement(s);
}

while中的continue使用案例如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/perl/03condition$ cat continue.pl
#!/usr/bin/perl
$a=0;
while($a<3){
print "a=$a\n";
}continue{
$a=$a+1;
}
biotest@ubuntu:~/perl/03condition$ perl continue.pl
a=0
a=1
a=2

foreach中的continue使用如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
biotest@ubuntu:~/perl/03condition$ cat continue2.pl
#!/usr/bin/perl
@list=(1,2,3,4);
foreach $a(@list){
print "a=$a\n";
}continue{
last if $a==4;
}
biotest@ubuntu:~/perl/03condition$ perl continue2.pl
a=1
a=2
a=3
a=4

(13.4)0redo语句

redo语句直接转到循环体的第一行开始重复执行本次循环,redo语句之后的语句不再执行,continue语句块也不再执行。continue语句可用在while和foreach循环中。
语法格式如下所示:

1
redo [LABEL]

其中LABEL是可选的。带标号修饰符LABEL的redo语句表示把循环控制流程直接转到与标号修饰符LABEL相关联的语句块的第一行处开始执行,而不再执行redo语句之后的语句和continue语句块;不带标号修饰符LABEL的redo语句表示把循环控制流程直接转到当前语句块的第一行处开始执行,而不再执行redo语句之后的语句和continue语句块;如果是在for循环中或者是带有continue语句块,则for循环中的递增列表和continue语句块都不再被执行,看下面的案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
biotest@ubuntu:~/perl/03condition$ cat redo.pl
#!/usr/bin/perl
$a=0;
while($a<10){
if($a==5){
$a=$a+1;
redo;
}
print "a=$a\n";
}continue{
$a=$a+1;
}
biotest@ubuntu:~/perl/03condition$ perl redo.pl
a=0
a=1
a=2
a=3
a=4
a=6
a=7
a=8
a=9

从结果来看,当a=5时,重新转到第一行运行。

(13.5)goto语句

Perl 有三种 goto 形式:got LABLE,goto EXPR,和 goto &NAME,如下所示:

序号 goto 类型
1 goto LABEL-找出标记为 LABEL 的语句并且从那里重新执行。
2 goto EXPR-goto EXPR 形式只是 goto LABEL 的一般形式。它期待表达式生成一个标记名称,并跳到该标记处执行。
3 goto &NAME-它把正 在运行着的子进程替换为一个已命名子进程的调用。

语法格式如下所示:

1
2
3
4
5
goto LABEL
goto EXPR
goto &NAME

使用案例如下所示:

在下面的这个案例中,当a=15时,跳出循环,进入LOOP这个标签处,此时a=16,继续循环。

1
2

下面的这个案例使用了goto EXPR的形式,它其实就是上一下案例的变形,只是中间的LABEL变成了一个表达式,本质还是一样的,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
biotest@ubuntu:~/perl/03condition$ cat gotoexpr.pl
#!/usr/bin/perl
$a=10;
$str1="LO";
$str2="OP";
LOOP:do
{
if($a==15){
$a=$a+1;
goto $str1.$str2; # this is similar to goto LOOP
}
print "a=$a\n";
$a=$a+1;
}while($a<20);
biotest@ubuntu:~/perl/03condition$ perl gotoexpr.pl
a=10
a=11
a=12
a=13
a=14
a=16
a=17
a=18
a=19

(14)错误处理

程序运行过程中,总会碰到各式各样的错误,比如打开一个不存在的文件。程序运行过程中如果出现错误就会停止,我们就需要使用一些检测方法来避免错误,从而防止程序退出。

(14.1) if语句判断错误

if语句可以用于判断返回值,使用格式如下所示:

1
2
3
4
5
if(open(DATA, $file)){
...
}else{
die "Error:Can not open file - $!";
}

如果文件打不开,就会返回错误信息,错误信息包含在变量$!中,上述代码也可以简化为以下的代码:

1
open(DATA,$file)||die "Error:Can not open file - $!";

(14.2)unless函数判断错误

unless函数与if相反,只有在表达式返回false时才会执行,使用方法如下所示:

1
2
3
unless(chdir("/etc"){
die "Error: Can not open directionary-$!";
}

同样也可以简写为:

1
die "Error:Can not open directionary-$!";

以上错误信息只有在目录切换出现错误的情况下才会出现。

(14.3)三元运算符

以下是三元运算符的使用案例:

1
print(exists($hash{value})?'Exist':'No Exist',"\n");

exists函数用于判断hash里面有没有对应的key,如果存在,返回True,不存在返回False。三元运算符的基本格式就是格式为表达式 ? 值一 : 值二。

(14.4)warn函数

warn函数用于触发一个警告信息,不会有其他操作,输出到STDERR(标准输出文件),通常用于给用户提示,使用方法如下所示:

1
chdir('/etc') or warn "Can not change directionary";

(14.5)die函数

die函数类似于warn, 但它会执行退出。一般用作错误信息的输出,使用方法如下所示:

1
chdir('/etc') or die "Can not change directionary";

(14.6)Carp模块

在Perl脚本中,报告错误的常用方法是使用warn()die()函数来报告或产生错误。而对于Carp模块,它可以对产生的消息提供额外级别的控制,尤其是在模块内部。标准Carp模块提供了warn()die()函数的替代方法,它们在提供错误定位方面提供更多信息,而且更加友好。当在模块中使用时,错误消息中包含模块名称和行号。

(14.6.1) carp函数

carp函数可以输出程序的跟踪信息,类似于warn函数,通常会将该信息发送到STDERR,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
biotest@ubuntu:~/perl/03condition$ cat carp.pl
#!/usr/bin/perl
package T;
require Exporter;
@ISA=qw/Exporter/;
@EXPORT=qw/function/;
use Carp;
sub function{
carp "Error in module!";
}
1;
use T;
function();
biotest@ubuntu:~/perl/03condition$ perl carp.pl
Can't locate T.pm in @INC (you may need to install the T module) (@INC contains: /home/biotest/miniconda2/lib/perl5/site_perl/5.22.0/x86_64-linux-thread-multi /home/biotest/miniconda2/lib/perl5/site_perl/5.22.0 /home/biotest/miniconda2/lib/perl5/5.22.0/x86_64-linux-thread-multi /home/biotest/miniconda2/lib/perl5/5.22.0 .) at carp.pl line 14.
BEGIN failed--compilation aborted at carp.pl line 14.

Carp模块里面好多代码不懂,先空起来,有空再来补充笔记。

参考资料

Perl教程|菜鸟教程