Perlの制御文
==, != #等しい、等しくない >,>=,<,<= #大小比較文字列の比較には、次の記号演算子を利用します。
eq,ne #等しい、等しくない
lt,le,gt,ge #小さい、以下、大きい、以上
cmp #比較し、小さい、同じ、大きい、とき、-1,0,1 を返す
文字列の比較には、他に正規表現に基づくパターンマッチがあります。マッチに成功すると真になります。
=~ #パターンマッチ
if (条件) {<文1>;} elsif (条件) {<文2>;} elsif (条件) {<文3>;} else {<文4>;}言語Cと異なり、{..}は必須ですから、注意してください。
print "your name?\n";
$name=<STDIN>;
chop($name);
if ($name eq "Itoh"){
print " Aya\n";}
else{
print "Taro\n";}
Cのswitchに対応する構文はありません。
print " Hello World !! " if $a == 1 ;
$i = 1;
while ($i < 10) {
print "i = $i\n";
$i++;
}
@name_list=("Aya","Yumi","Tae");
$i=0;
while($i != 3){
print "$namelist[$i]\n";
$i++;
}
#while
@list=("pen",12,"eraser",7,"tape",10,"clip",7);
$total=0;
while($st=shift(@list)){
$price=shift(@list);
print "item=$st:price=$price\n";
$total += $price;
}
print "total=$total\n"
実行結果item=pen:price=12 item=eraser:price=7 item=tape:price=10 item=clip:price=7 total=36
#foreach
@list=("pen","eraser","tape","clip");
foreach $val(@list){
print "$val\n";
}
while ( 1 ) {
if ( $a == 1 ) { last ; }
}