perl 5.10后的新特征 given-when介绍
来源:互联网 发布:js循环给数组赋值 编辑:程序博客网 时间:2024/05/16 13:48
given语句
Perl中的given-when控制结构能够根据given的参数,执行某个条件对应的语句块,其与C语言中的switch语句类似。只不过更具有Perl的色彩。看看下面的例子,其从命令行中取出第一个参数,$ARGV[0],然后依次走一遍when条件判断,看是否找到了Fred。每个when语句对应不同的处理方式,判断的条件从最宽松的开始测试:
use5.010;
given( $ARGV[0]) {
when( /fred/i) { say 'Name has fred in it' }
when( /^Fred/) { say 'Name starts with Fred' }
when( 'Fred' ) { say 'Name is Fred'}
default { say "I don't see a Fred"}
}
--given会将参数化为$_,每个when条件都尝试用智能匹配对$_进行测试,实际上可以写成如下形式,这就清除多了:
use5.010;
given( $ARGV[0]) {
when( $_ ~~ /fred/i ) { say 'Name has fred in it'}
when( $_ ~~ /^Fred/ ) { say 'Name starts with Fred'}
when( $_ ~~ 'Fred' ){ say 'Name is Fred'}
default { say "I don't see a Fred"}
}
--如果$_不能满足任何when条件,perl就会执行default语句块。下面为运行结果:
$ perl5.10.0 switch.pl Fred
Name has fred in it
$ perl5.10.0 switch.pl Frederick
Name has fred in it
$ perl5.10.0 switch.pl Barney
I don't see a Fred
$ perl5.10.0 switch.pl Alfred
Name has fred in it
看到这里你也许会说,Perl中的if-elsif-else语句不是一样可以完成这个例子吗,为什么还要用given-when语句呢,它还有存在的必要吗?实际上他们两个语句最大的不同在于given-when可以在满足某个条件的基础上,继续测试其他的条件,但if-elsif-else一旦满足了某个条件,就只能执行对应的那个语句块。实际上前面的例子可以写成如下的方式:
use5.010;
given( $ARGV[0]) {
when( $_ ~~ /fred/i ) { say 'Name has fred in it'; break}
when( $_ ~~ /^Fred/ ) { say 'Name starts with Fred'; break}
when( $_ ~~ 'Fred' ){ say 'Name is Fred'; break}
default { say "I don't see a Fred"; break}
}
--按照这种写法,因为第一条测试语句如果传来的参数匹配/fred/i,后面的所有语句就没有机会执行了,这时候就会直接跳出控制结构。如果在when语句块的末尾使用continue,Perl就会尝试执行后续的when语句了,这也是if-elsif-else语句块力不能及的地方。当另一个when的条件满足时,会执行对应语句块。在每个when语句块的末尾写上continue,就意味着所有的条件判断都会执行:
use5.010;
given( $ARGV[0]) {
when( $_ ~~ /fred/i ) { say 'Name has fred in it';continue }
when( $_ ~~ /^Fred/ ) { say 'Name starts with Fred';continue }
when( $_ ~~ 'Fred' ){ say 'Name is Fred';continue } # 注意!
default { say "I don't see a Fred"}
}
--实际上这样写是有问题的,这里的default总是会运行
$ perl5.10.0 switch.pl Alfred
Name has fred in it
I don't see a Fred
--default块相当于一个测试条件永远为真的when语句。如果在default之前的when语句使用了continue,Per就会继续执行default语句。因此可以说default就是一个特殊的when:
use5.010;
given( $ARGV[0]) {
when( $_ ~~ /fred/i ) { say 'Name has fred in it';continue }
when( $_ ~~ /^Fred/ ) { say 'Name starts with Fred';continue }
when( $_ ~~ 'Fred' ){ say 'Name is Fred';continue } # 注意!
when( 1 == 1 ){ say "I don't see a Fred"} # 相当于default语句块
}
要解决这个问题,只要拿掉最后一个when的continue就可以了,改写成如下形式:
use5.010;
given( $ARGV[0]) {
when( $_ ~~ /fred/i ) { say 'Name has fred in it';continue }
when( $_ ~~ /^Fred/ ) { say 'Name starts with Fred';continue }
when( $_ ~~ 'Fred' ){ say 'Name is Fred'; break} # 现在就对了!
when( 1 == 1 ){ say "I don't see a Fred"} # 这里的when(1==1)可改写成default
}
多个项目的when匹配
有些时候需要遍历很多元素,但given只能一次接受一个参数,当然可以将given语句放到foreach里面循环测试。比如要遍历@names,依次将各元素赋值到$name,然后再用given:
use5.010;
foreach my $name ( @names) {
given( $name ) {
...
}
}
如果使用given-when语句,想必大家首先想到的就是上面的方法,实际上要遍历多个元素时就就不必使用given了,使用foreach的简写形式,让它给当前正在遍历的元素起个化名$_。此外若要用智能匹配,当前元素就只能是$_。
use5.010;
foreach ( @names ) {# 不要使用命名变量!
when( /fred/i) { say 'Name has fred in it'; continue }
when( /^Fred/) { say 'Name starts with Fred'; continue }
when( 'Fred' ) { say 'Name is Fred';}
default { say "I don't see a Fred"}
}
一般在遍历的时候,总希望可以看到当前的工作状态。可以在foreach语句块中写上其他的语句,比如say:
use5.010;
foreach ( @names ) {# 不要使用命名变量!
say "/nProcessing $_";
when( /fred/i) { say 'Name has fred in it'; continue }
when( /^Fred/) { say 'Name starts with Fred'; continue }
when( 'Fred' ) { say 'Name is Fred';}
say "Moving on to default...";
default { say "I don't see a Fred"}
}
- perl 5.10后的新特征 given-when介绍
- perl 5.10后的新特征 given-when介绍
- Perl 学习笔记(10)-- 智能匹配和given-when
- Perl笔记:Perl语言入门(第六版) - 智能匹配、given-when
- perl given与C switch的区别
- Perl语言入门(第五版) 读书笔记(九)---智能匹配与given-when结构
- Perl语言入门笔记 第十五章 智能匹配与given-when结构
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- iOS6新特征:UICollectionView介绍
- php递归转义数组
- Java提取网页中url的正则表达式
- javascript对页面简单的加密和解密
- python版CSDN博客备份工具
- Net::SSH::Perl 安装问题解决
- perl 5.10后的新特征 given-when介绍
- 细节很关键
- JAVA_OPTS参数说明与配置
- 解析elasticsearch的config下的配置文件
- 三个故事——说穿很多人
- 树形控件(Tree Control)
- vlc-android打开rtsp流媒体文件函数调用过程
- Android使用FileObserver对sdcard文件或文件夹监控
- switch case 在C中的编译链接规则