Chinaunix首页 | 论坛 | 博客
  • 博客访问: 928421
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: Python/Ruby

2011-06-29 09:44:20

  1. 1.调取子程序,将sun 子程序不断相加,然后面调取值。
  2. #!/usr/bin/perl -w
  3. sub feiyang {
  4.                   $n =1;#全局变量
  5.                   print "sailor number $n!\n"
  6.                  }
  7. &feiyang;#第一次调取子程序
  8. &feiyang;#第二次
  9. sub sum_f_b{
  10.   print "you called the sum_f_b subroutine!\n";
  11.   $fred $barney;
  12. }
  13. #给变量赋值
  14. $fred = 3;
  15. $barney = 2;
  16. $wsum = &sum_f_b;
  17. print "\$wsum is $wsum. \n";
  18. $betty = 3 * &sum_f_b;
  19. print "\$betty is $betty \n";
  20. 输出结果:
  21.  
  22. ]# perl sun.pl
  23. sailor number 1!
  24. sailor number 2!
  25. you called the sum_f_b subroutine!
  26. $wsum is 5.
  27. you called the sum_f_b subroutine!
  28. $betty is 15
  29.  
  30. 2.向子程序传递参数
  31. cat suncan.pl
  32. sub max{
  33.  if ($_[0]>$_[1]){# 数组为第一个参数的索引值为0,以此类推。
  34.    $_[0];
  35.    }else {
  36.    $_[1]
  37.    }
  38. }
  39. $n = &max(1,2);#将1和2传递给max 程序进行对比,然后获取返回值打印出来
  40. #&max;
  41. print $n;
  42. 输出结果:
  43. #]#perl suncan.pl
  44. 2

  45. 3.子程序中的私有变量
  46. #Perl 默认创建的是全局变量,在任何地方都可以访问,用my 操作符创建私有变量,禁止全局访问。
  47. #@_ 包含参数列表
  48. #!/usr/bin/perl -w
  49. sub max{
  50. #my ($m,$n); #私有变量
  51. #($m,$n)=@_;
  52. my ($m,$n) = @_; #等同于 上面注释的两行
  53. if($m > $n){ $m}else{$n}
  54. }

  55. 完整例子:

  56. #!/usr/bin/perl -w

  57. sub max{
  58. my ($m,$n) = @_; #== my ($m,$n); ($m,$n)=@_;
  59. if($m > $n)
  60. { print "the \$m is :$m \n";}else{print "the \$n is:$n \n";}
  61. }

  62. &max(4,3);#为子程序传递参数,这时@_或默认读取这两元素自定义为数组,然后将其赋值给变量。
  63. 输出:
  64. ]# perl sunmy.pl
  65. the $m is :4
4.可变的参数列表
#可以用if判断@_ 参数个数,因为Perl 默认会把任意长度的列表当做参数传给子程序,但这也会造成很多问题。
#上边完善后的脚本为
sub max{
  #判断是否为两个,两个就进行比较。
  if (@_ !=2){
       $sum = @_; #定义变量来获取@_的参数个数。
        print "Warning ! The \@_ numbers is $sum, &max should be two arguments!\n";
     }
  else{
      my ($m,$n) = @_;
      if($m > $n)
      { print "the \$m is :$m \n";}else{print "the \$n is:$n \n";}
      }
}

#&max(4,3,5);# 分别用两个参数和三个参数的子程序 做实验,
&max(4,3);

输出:
当参数为三个时:
]# perl sunmy.pl
Warning ! The @_ numbers is 3, &max should get exactly two arguments!
当参数为两个时:
]# perl sunmy.pl
the $m is :4

5.更好的&max 子程序
$maxmum = &max(3,5,10,4,2);
sub max {
 #  print "The \$maxmum is : $maxmum ";
   my ($myx_so_far) = shift @_;
   foreach (@_){
       if($_ > $max_so_far){
          $max_so_far = $_;
        }
    }
   print "The \$max_so_far is:\n $max_so_far \n";
}

6.my参数


#!/usr/bin/perl -w
foreach(1...10){
 my ($square)= $_ * $_;
 print "$_ squared is $square.\n";
}

7.return
#!/usr/bin/perl -w
my @names = qw/ aa bb cc dd ee ff gg hh kk /;
my $result = &which_element_is("cc",@names);
sub which_element_is {
  my($what,@array)= @_;
  foreach $once (0...$#array){
     if ($what eq $array[$once]){
       return $_;
       #print "the what is :\n $what \n:the \@array is :@array.\n";
      }
 
   }
  -1;
}
print "the result is :\n $result \n" ;

8.非标量返回值
#!/usr/bin/perl -w
sub list_f_fred_to_barney{
    if($fred < $barney){
    $fred..$barney;   
    } else {
    reverse $barney..$fred;
        }
    return $c;
}
$fred= 11;
$barney = 6;
$c = &list_f_fred_to_barney;
print $c;
阅读(1016) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~