Perl语言入门——智能匹配与given-wh…

智能匹配操作符:新的智能匹配操作符(~~)会根据需要自己决定用何种方式比较两端的操作数
      以下罗列一些例子
     
      例一
      print "I found Fred in the name!\n" if $name=~/Fred/;
      say "I found Fred in the name!\n" if $name ~~ /Fred/;
     
      例二
      my $flag=0;
      foreach my $key(keys %names){
              next unless $key=~/Fred/;
              $flag=$key;
              last;
      }
      print "I found a key matching 'Fred'. It was $flag\n" if $flag;
      say "I found a key matching 'Fred'" if %names ~~ /Fred/;
     
      例三
      my $equal=0;
      foreach my $index (0 .. $#names1){
              last unless $names1[$index] eq $names2[$index];
              $equal++;
      }
      print "The arrrays have the same elements\n" if $equals==@names1;
      say "The arrays have the same elements\n" if @names1 ~~ @names2;
      使用智能匹配时,对两边操作数的顺序没有要求,倒过来写也行,且满足“交换律”
智能匹配操作的优先级:perlsyn在线手册中“Smart matching in detail”部分
given语句:given-when控制结构能够根据given的参数,执行某个条件对应的语句块,对应C语言的switch
      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条件都尝试用智能匹配对$_进行测试
      如果when语句块的末尾使用continue,perl就会尝试执行后续的when语句,意味着所有的条件判断都会执行
      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,可以将最后一个continue去掉
笨拙匹配:除了在given-when中使用智能匹配外,还可以像前面那样使用所谓笨拙匹配
      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'}
              default {say 'I don't see a Fred'}
      }
      甚至可以混用笨拙匹配和智能匹配
多个项目的when匹配:有时候需要遍历许多元素,可given智能一次接受一个参数
      因此可以使用foreach简写方式,让它给当前正在遍历的元素起个化名$_
      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'}
      }
      此外,若要用智能匹配,当前元素只能是$_
      另外,可以在when语句之间写上其他语句用于调试
      foreach (@names){
              say "\nProessing $_";
              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'}
           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值