发表于 2018-8-30 11:06:58

perl自学笔记整理(三)

  关于perl中的子程序
  1、定义子程序
  sub sub_programe_name {
  程序语句体;
  }
  2、调用子程序
  &sub_programe_name;
  3、子程序的返回值
  在子程序的执行过程中,它会不断进行运算。而最后一次运算的结果(不管是什么),都会被自动当成子程序的返回值。
  # cat a4.pl
  #!/usr/bin/perl
  #subroutine
  sub sum_of_fred_and_barney {
  print "Hey , you called the sum_of_fred_and_barney subroutine!\n";
  $fred + $barney;#这是该子程序的最后一次运算,其结果将成为本子程序的返回值。
  }
  ($fred, $barney) = qw( 3 4 );
  $wilma = &sum_of_fred_and_barney;
  print "\$wilma is $wilma.\n";
  $betty = 3 * &sum_of_fred_and_barney;
  print "\$betty is $betty.\n";
  # perl a4.pl
  Hey , you called the sum_of_fred_and_barney subroutine!
  $wilma is 7.
  Hey , you called the sum_of_fred_and_barney subroutine!
  $betty is 21.
  4、向子程序中传递参数
  $n = &max(10, 15);      #包含两个参数的子程序调用
  参数列表将会被传入子程序,让子程序随意使用。当然,得先将这个列表存在某处,Perl会自动将参数列表化名为特殊的数组变量@_ , 改变量在子程序执行期间有效。子程序可以访问这个数组,以判断参数的个数以及参数的值。 @_ 变量是子程序的私有变量。假如已经有了全局变量@_ , 则该变量在子程序调用前会先被存起来,并在子程序返回时恢复原本的值。
  这表示子程序的第一个参数存储于$_,第二个参数存储于 $_,依次类推。
  5、长度可变的参数列表
  例如:求任意数中的最大值
  # cat test.pl
  #!/usr/bin/perl
  #test
  $max_num = &max( 1, 11, 11, 15, 7, 10 );
  sub max {
  my $num_max = shift @_;
  foreach (@_) {
  if ( $_ > $num_max ) {
  $num_max = $_;
  }
  }
  $num_max;
  }
  print "The max number is : $max_num\n";
  # ./test.pl
  The max number is : 15
  例如二:求任意数的最小值
  # cat test.pl
  #!/usr/bin/perl
  #test
  $max_num = &max( 1, 11, 11, 15, 7, 10 );
  sub max {
  my $num_max = shift @_;
  foreach (@_) {
  if ( $_ < $num_max ) {
  $num_max = $_;
  }
  }
  $num_max;
  }
  print "The min number is : $max_num\n";
  # ./test.pl
  The min number is : 1
  6、关于词法my变量
  my $aaaa= *****;    #不在任何语句块中的my管全局变量
  foreach (@bbbbb) {
  -----------
  -----------
  my $ccccc;      #在语句块中的my变量只对当前语句块生效,跳出后就恢复原有。
  }
  my($num) = @_;      #列表上下文, 和 ($num) = @_; 相同
  my $num = @_;   #标量上下文, 和 $num = @_; 相同
  my $fred, $barney;      #错! 没声明 $barney
  my ($fred, $barney);    #两个都声明了。
  my @new_number;      #你也可以用my来创建新的私有数组;所有新变量的值一开始都是空的:标量被设为undef, 数组被设为空列表。
  7、return操作符
  return操作符会从子程序中立即返回某个值,而不再执行子程序的其余部分。
  例如:
  # cat test12.pl
  #!/usr/bin/perl
  #test12
  my @test = qw( beijing shanghai guangzhou shenzhen );
  my $result = &aa( guangzhou, @test);
  sub aa {
  my ($what, @array) = @_;
  foreach (0..$#array) {
  if ($what eq @array[$_]) {
  return $_;            #如果找到下标值就立即返回$_的值;
  }
  }
  -1;      #其实这里的-1等同于return -1; 不过在此程序员省略了而已。
  }
  print "guangzhou 的下标值是: $result \n"
  # ./test12.pl
  guangzhou 的下标值是: 2
  8、子程序中的非标量返回值,比如返回一个列表值。
  例如:
  # cat test13.pl
  #!/usr/bin/perl
  #test13
  sub max_to_min {
  $fred = 16;
  $barney = 10;
  if ($fred < $barney) {
  reverse $fred .. $barney;
  }else{
  reverse $barney .. $fred;
  }
  }
  @cc = &max_to_min;
  print "@cc\n";
  # ./test13.pl
  16 15 14 13 12 11 10
  9、持久性私有变量--state
  在子程序中可以使用my操作符来创建私有变量,但每次调用这个子程序的时候,这个私有变量都会被重新定义。使用state操作符来声明变量,我们便可以在子程序的多次调用间保留变量的值,并将变量的作用域局限于子程序中。
  # cat test14.pl
  #!/usr/bin/perl
  #test14
  use 5.010;            #此处特别注意,如果不用5.10版,会导致state @numbers;行报错!!
  &running_sum( 5, 6 );
  &running_sum( 1..3 );
  &running_sum( 4 );
  sub running_sum {
  state $sum = 0;
  state @numbers;
  foreach my $number ( @_ ) {
  push @numbers, $number;
  $sum += $number;
  }
  print "The sum of (@numbers) is $sum\n";
  };
  # ./test14.pl
  The sum of (5 6) is 11
  The sum of (5 6 1 2 3) is 17
  The sum of (5 6 1 2 3 4) is 21

页: [1]
查看完整版本: perl自学笔记整理(三)