现在的位置: 首页 > 综合 > 正文

第一天学PERL

2014年01月10日 ⁄ 综合 ⁄ 共 1257字 ⁄ 字号 评论关闭

前几天在机器上装了一个WINDOWS版的PERL环境,然后开始研究了一下PERL

1、在做HELLO WORLD测试时,输出结果中有错误,如下:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
不经意间从别的例子中考了一行过来,就解决这个问题了,加上如下一行:
print "Content-type:text/html","/n/n";#设置输出格式为html格式
2、字符串的重复操作:"abc"x3的输出结果为:"abcabcabc";一开始把x当成*了,后来才发现用*才是数学运算
3、数字--字符可自动转换
4、函数的返回值不需要声明,直接输出就行。也不需要输入变量,直接引用函数外面的变量。

    sub sum_of_fred_and_barney {
      print "Hey, you called the sum_of_fred_and_barney subroutine!/n";
      $fred + $barney;  # That's the return value
    }
    $fred = 3;
    $barney = 4;
    $wilma = &sum_of_fred_and_barney;      # $wilma gets 7
    print "/$wilma is $wilma./n";
    $betty = 3 * &sum_of_fred_and_barney;  # $betty gets 21
    print "/$betty is $betty./n";

但是如果在函数里多加一行的话,那么返回值则为1,表示输出信息成功,不会返回计算的值,所以要注意这点,如下:
    sub sum_of_fred_and_barney {
      print "Hey, you called the sum_of_fred_and_barney subroutine!/n";
      $fred + $barney;  # That's not really the return value!
      print "Hey, I'm returning a value now!/n";      # Oops!
    }

也可以用条件语句来判断返回值,如下:
    sub larger_of_fred_or_barney {
      if ($fred > $barney) {
        $fred;
      } else {
        $barney;
      }
    }
   
函数传递参数时,不需要在方法中声明,直接在方法体中引用_[0],_[1]....就可以了,如下
    sub max {
      # Compare this to &larger_of_fred_or_barney
      if ($_[0] > $_[1]) {
        $_[0];
      } else {
        $_[1];
      }
    }

问题:"/n"不换行。 

抱歉!评论已关闭.