Perl Reference,传递一个计数变量($#)是如何得到完整数据的引用的?

Perl Reference, how is the passing a count variable($#) get the reference of complete data?

下面是我的代码

sub print_list {
  $max = $_[0];
  for ($i=0; $i<$max; $i++)
  {
    print "$i.  $list[$i][0]\t $list[$i][1]\n";
  }
}

# Declaring a 2-D Array, which is just an array of 1-D arrays

@list = ( ["vi   ", "Null"], ["emacs", "Null"], ["joe  ", "Null" ]);

$max = $#list + 1;

print "Initial Values\n";
print_list($max);

print "\n\n";

输出:-

 Initial Values 
 0. vi                 Null 
 1. emacs           Null 
 2. joe               Null 

在上面的代码中,传递给函数的$max是如何获取函数中所有可用数据的。

将$max 传递给函数不会同时将@list 数组传递给函数。不传递任何内容来证明这一点:

sub print_list {
  for ($i=0; $i<$max; $i++)
  {
    print "$i.  $list[$i][0]\t $list[$i][1]\n";
  }
}

# Declaring a 2-D Array, which is just an array of 1-D arrays

@list = ( ["vi   ", "Null"], ["emacs", "Null"], ["joe  ", "Null" ]);

$max = $#list + 1;

print "Initial Values\n";
print_list();

print "\n\n";

@list 数组变量是全局变量。

perldoc perlsub

@list 数据根本没有传递到子例程中。您正在通过在子例程之外可见的相同版本的 @list 访问它。访问未传递到您的子例程中的变量是一个非常糟糕的主意,因为您的子例程与调用它的代码耦合得太紧密了。例如,在您的代码中,print_list 只能作用于名为 @list 的变量。如果您将数组传递给子例程,那么它的名称就无关紧要了。

# I've renamed this as you're dealing with arrays, not lists
sub print_array {
  my $length = shift;
  my @array  = @_;

  for ($i = 0; $i < $max; $i++) {
    print "$i.  $array[$i][0]\t $array[$i][1]\n";
  }
}

my @editors = ( ["vi   ", "Null"], ["emacs", "Null"], ["joe  ", "Null" ]);
my $number_of_editors = @editors; # More readable than $#editors + 1

print_array($number_of_editors, @editors);

但是你根本不需要将数组长度传递给子程序。

# I've renamed this as you're dealing with arrays, not lists
sub print_array {
  my @array  = @_;

  for ($i = 0; $i < @array; $i++) {
    print "$i.  $array[$i][0]\t $array[$i][1]\n";
  }
}

my @editors = ( ["vi   ", "Null"], ["emacs", "Null"], ["joe  ", "Null" ]);

print_array(@editors);

作为最后的改进,foreach 循环几乎总是比 C 风格的 for 循环更容易理解

# I've renamed this as you're dealing with arrays, not lists
sub print_array {
  my @array  = @_;

  foreach (0 .. $#array) {
    print "$_.  $array[$_][0]\t $array[$_][1]\n";
  }
}