栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

perl学习笔记(十六)given-when语句

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

perl学习笔记(十六)given-when语句

文章目录
  • 前言
  • 16 given-when语句
  • 总结


前言

本文主要记录一下,perl中的given-when语句。

perl中的given-when语句相当于C语言中的switch-case语句。

  • given-when的语法
  • continue的应用

16 given-when语句
#=====================================
# given-when语句,相当于C语言中的switch-case
#=====================================
use 5.010;

my $name = $ARGV[0];

   if($name =~ /fred/i) { say 'if: Name has fred'}
elsif($name =~ /^Fred/) { say 'if: Name starts with Fred'}
elsif($name eq 'fred')  { say 'if: Name equal fred'}
else                    { say 'if: I dont see fred'}
# 上边的if和下边的given-when等效
given($name){
    when(/fred/i) { say 'if: Name has fred'}
    when(/^Fred/) { say 'if: Name starts with Fred'}
    when('fred')  { say 'if: Name equal fred'}
    default       { say 'if: I dont see fred'}
}

# 加上continue后下边的语句会继续执行,默认是break
given($name){
    when(/fred/i) { say 'if: Name has fred'; continue}
    when(/^Fred/) { say 'if: Name starts with Fred'; continue}
    when('fred')  { say 'if: Name equal fred'}
    default       { say 'if: I dont see fred'}
}

given($ARGV[1]){
    when( /^-?d+$/){ #匹配数字
        say 'This is a num!'; continue
    }
    when($_ > 10){
        say 'Num is greater than 10'
    }
}

总结
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001458.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号