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

2011年(56)

2010年(174)

2009年(15)

分类: Python/Ruby

2011-06-27 18:19:43

  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

  66. 4.可变的参数列表
  67. #可以用if判断@_ 参数个数,因为Perl 默认会把任意长度的列表当做参数传给子程序,但这也会造成很多问题。
  68. #上边完善后的脚本为
  69. sub max{
  70.   #判断是否为两个,两个就进行比较。
  71.   if (@_ !=2){
  72.        $sum = @_; #定义变量来获取@_的参数个数。
  73.         print "Warning ! The \@_ numbers is $sum, &max should be two arguments!\n";
  74.      }
  75.   else{
  76.       my ($m,$n) = @_;
  77.       if($m > $n)
  78.       { print "the \$m is :$m \n";}else{print "the \$n is:$n \n";}
  79.       }
  80. }

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

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

  90. 5.更好的&max 子程序
  91. $maxmum = &max(3,5,10,4,2);#为子程序定义一个数组
  92. sub max {
  93.    my ($myx_so_far) = shift @_;#shift 窃取数组第一个元素,并输出
  94.    foreach (@_){ #遍历数组
  95.        if($_ > $max_so_far){
  96.           $max_so_far = $_; #进行比对后,如果$_ 中有比截取出来的大,赋值给$max_so_far,直到条件不成立为止
  97.         }
  98.     }
  99.    print "The \$max_so_far is:\n $max_so_far \n";
  100. }
  101. 输出:
  102. The $max_so_far is:
  103.  10

  104. 5.my变量的使用
  105. my($num)= @_; #列表上下文,和($num) = @_; 相同
  106. my $num = @_; #标量上下文,和$num = @_; 相同
  107. 第一个,按照列表上下文,$num 会被设为第一个参数,
  108. 第二个,按照标量上下文,它会被设为参数的个数。
  109. my不适用括号时,只用来声明单个词法变量:
  110. my $fred, $barney ;#错! 没声明$barney
  111. my($fred, $barney); #两个都声明了。
  112. 例:
  113. #!/usr/bin/perl -w
  114. foreach(1...10){
  115.  my ($square)= $_ * $_;
  116.  print "$_ squared is $square.\n";
  117. }


  118. 输出:
  119. ]# perl sunzero.pl
  120. 1 squared is 1.
  121. 2 squared is 4.
  122. 3 squared is 9.
  123. 4 squared is 16.
  124. 5 squared is 25.
  125. 6 squared is 36.
  126. 7 squared is 49.
  127. 8 squared is 64.
  128. 9 squared is 81.
  129. 10 squared is 100.
6.非标量值返回
#!/usr/bin/perl -w
sub list_f_fred_to_barney{
        if($fred < $barney){
        $fred..$barney;
        } else {
        reverse $barney..$fred;
        }
}
$fred= 11;
$barney = 6;
$c = &list_f_fred_to_barney;
print $c;
输出:
]# perl feibiao.pl
11019876

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" ;#验证输出的值,return

8.数组相加得值
#!/usr/bin/perl -w
my @fred= qw(1 3  5  7 9);
my $fred_total = total(@fred); #此处省略了&符号
sub total{
  my $sum = 0;
  foreach (@_){
   $sum += $_;

   }
$sum
}
print "The total of \@fred is $fred_total.\n";

9.从1加到1000
#!/usr/bin/perl -w
my @fred= qw(1 3  5  7 9);
my $fred_total = total(@fred);
sub total{
  my $sum = 0;
  foreach (1..1000){ #会覆盖之前fred数组的元素
   $sum += $_;

   }
$sum
}
print "The total of \@fred is $fred_total.\n";


阅读(1064) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~