close

#!/usr/bin/perl
use Switch; #must use switch when you use switch case operation
#chmod a+x control.pl
$zero = 0;
$one = 1;
$two = 2;
$three = 3;
$float_zero = "0.0";


$str_one = "one";
$str_two = "two";
#if
printf "===== if...else =====";
printf "if...else (int)\n";
if( $one eq $two ){
        printf("int equal\n");
}else{
        printf("int not equal\n");
}


printf "if...else (str)\n";
if ( $str_one eq $str_two ){ #note: use == get same result
        printf "str equal\n";
}else{
        printf "str not equal\n";
}


print "if...else (int true)\n";
if($one){
        printf "int true\n";
}else{
        printf "int false\n";
}


printf "Special case if...else (sting true)\n";#note: string "0.0" is true, the float 0.0 is false
if($float_zero){
        printf "float true\n";
}else{
        printf "float false\n";
}


#for
printf "===== for... =====\n";
@str_ary = ("one","two","three","four");
for ($loop=0;$loop<$two;$loop++){
        printf("for $str_ary[$loop]\n");
}
printf "===== while... =====\n";
#while
while( $three ne 0){
        printf "while test ".$three."\n";
        $three -= 1;
}


#condition operator
#perl | c
#---------------
#next | continue
#last | break
$three = 3;
while ( $three ne 0){
        if( $three == 1){
                printf "break while test"."\n";
                last;
        }
        else{
                printf "break while test ".$three."\n";
                $three-=1;
        }
}
printf "===== switch...case =====\n";
printf "switch case\n";#note: at file head must "use Switch"
$three=3;
for ($loop=0;$loop<$three;$loop++){
        switch($loop){
                case 0 {printf "switch ".$loop."\n"}
                case 1 {printf "switch ".$loop."\n"}
                case 2 {printf "switch ".$loop."\n"}
        }
}

result


steven@ATM64:~/perl$ ./control.pl
===== if...else =====if...else (int)
int not equal
if...else (str)
str not equal
if...else (int true)
int true
Special case if...else (sting true)
float true
===== for... =====
for one
for two
===== while... =====
while test 3
while test 2
while test 1
break while test 3
break while test 2
break while test
===== switch...case =====
switch case
switch 0
switch 1
switch 2

arrow
arrow
    全站熱搜

    = = 發表在 痞客邦 留言(0) 人氣()