perl简单语句的修饰符
perl中常见的简单语句修饰符有 if unless while until foreach,它们demo语句分别如下: 1. if 条件修饰符 格式: #case1
$x=5;
print $x if $x==5; #====>输出5
#case2
$_="xabcyn";
print if /abc/; #====>输出xabcy
#case3
$_="I lost my gloves in the clover.";
print "Found love in gloves!n" if /love/;
#====>输出Found love in gloves!
#case4
my $i = 0;
while(1)
{
last if $i > 5; #当i>=5时,while循环结束
print "$i:$in";
$i++;
}
2. unless条件修饰符 格式: #demo
$x=5;
print $x unless $x==6; #====>输出5
3. while循环修饰符 格式: #demo
$x=1;
print $x++,"n" while $x!=5; #====>输出1,2,3,4
4. until 格式: #demo
$x=1;
print $x++,"/n" until $x==5; #====>输出1,4
5. foreach @alpha=(a .. z,"n");
print foreach @alpha; #====>abcdefghijkmnopqrstuvwxyz
总结 使用修饰符的写法会使程序更加紧凑,从一定程度减少了大括号的写法,这种简洁写法适用于大括号内的只有一句时才可以这么写。等价写法如下: $x=5;
#简洁写法
print $x if $x == 5;
#等价写法
if ($x == 5)
{
print $x;
}
from:https://www.cnblogs.com/wanming88/p/4143556.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |