您的位置:首页 > 其它

Something about Perl (4) 流程控制

2007-02-07 10:55 218 查看
unless结构
可以看作ifnot()

until
可以看作while_not()
适当使用这两种结构可以简化条件判断表达式

在perl中执行系统命令
$result = `/bin/ps`;

一个用perl写的脚本,用于杀死系统中后台运行的perl程序

      1 #!/usr/bin/perl -w
      2
      3 my $PS = `/bin/ps`;
      4
      5 if ($PS =~ m/^([0-9]+).*perl/m)
      6     {
      7     print $1."/n";
      8     my $ret=`kill -n 9 $1`; #Send terminate signal
      9     }
     10 print $PS;
只能杀掉第一个perl运行的程序

表达式修饰符
print "$n is a negative number./n" if $n < 0; #注意中间除了空格没有别的分隔符

    if ($n < 0) {
      print "$n is a negative number./n";
    }
等效
虽然条件表达式在书写上是在语句末尾,仍然整行中最先执行的!!

循环语句也可以写作这种形式
    $i *= 2 until $i > $j;
    print " ", ($n += 2) while $n < 10; #注意print和后一条语句之间是逗号!说明他们在同一个循环块中
后面一条语句等价于:
    while ($n < 10) {
      print " ", ($n += 2);
    }

foreach作为表达式修饰符使用时只能使用$_作为循环变量
如果需要自定义循环变量,只能用传统的写法

语句块中的局部变量仅在语句块中有效
    {
      print "Please enter a number: ";
      chomp(my $n = <STDIN>);
      my $root = sqrt $n;  # calculate the square root
      print "The square root of $n is $root./n";
    }
这里$n和$root在语句块外不再有效

注意在perl中elsif的拼写
    if ( ! defined $dino) {
      print "The value is undef./n";
    } elsif ($dino =~ /^-?/d+/.?$/) {
      print "The value is an integer./n";
    } elsif ($dino =~ /^-?/d*/./d+$/) {
      print "The value is a _simple_ floating-point number./n";
    } elsif ($dino eq '') {
      print "The value is the empty string./n";
    } else {
      print "The value is the string '$dino'./n";
    }

自增自减操作符
    my $m = 5;
    my $n = ++$m;   # increment $m to 6, and put that value into $n
    my $c = --$m;   # decrement $m to 5, and put that value into $c

    my $d = $m++;   # $d gets the old value (5), then increment $m to 6
    my $e = $m--;   # $e gets the old value (6), then decrement $m to 5

last操作符
直接跳出循环体,类似C语言中的break
    # Print all input lines mentioning fred, until the _ _END_ _ marker
    while (<STDIN>) {
      if (/_ _END_ _/) {
        # No more input on or after this marker line
        last;
      } elsif (/fred/) {
        print;
      }
    }
    ## last comes here ##

next操作符
跳过本轮以下所有语句,直接开始下一轮循环是否进行的判断,类似C语言中的continue
    # Analyze words in the input file or files
    while (<>) {
      foreach (split) {  # break $_ into words, assign each to $_ in turn
        $total++;
        next if //W/;    # strange words skip the remainder of the loop
        $valid++;
        $count{$_}++;    # count each separate word
        ## next comes here ##
      }
    }

    print "total things = $total, valid words = $valid/n";
    foreach $word (sort keys %count) {
      print "$word was seen $count{$word} times./n";
    }

redo操作符
返回本轮循环语句块顶端,在C语言中没有类似操作符
    # Typing test
    my @words = qw{ fred barney pebbles dino wilma betty };
    my $errors = 0;

    foreach (@words) {
      ## redo comes here ##
      print "Type the word '$_': ";
      chomp(my $try = <STDIN>);
      if ($try ne $_) {
        print "Sorry - That's not right./n/n";
        $errors++;
        redo;  # jump back up to the top of the loop
      }
    }
    print "You've completed the test, with $errors errors./n";

综合使用三个操作符的例子:
    foreach (1..10) {
      print "Iteration number $_./n/n";
      print "Please choose: last, next, redo, or none of the above? ";
      chomp(my $choice = <STDIN>);
      print "/n";
      last if $choice =~ /last/i;
      next if $choice =~ /next/i;
      redo if $choice =~ /redo/i;
      print "That wasn't any of the choices... onward!/n/n";
    }
    print "That's all, folks!/n";

逻辑与 && and
逻辑或 || or

利用逻辑语句设置默认值
my $last_name = $last_name{$someone} || '(No last name)';
如果someone没有在hash中,则被设置为No Last name

问号三元操作符
expression ? if_true_expr : if_false_expr
求平均值
   my $average = $n ? ($total/$n) : "-----";
    print "Average: $average/n";
如果n未定义或者为0, 结果为-----

利用and or操作符进行流程控制
($m < $n) && ($m = $n);
等价于if ($m < $n) { $m = $n }

而($m<$n) || ($m =$n);
等价于until ($m < $n) { $m = $n }

在perl中打开文件的常用方法:
    open CHAPTER, $filename
      or die "Can't open '$filename': $!";

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息