[英]Dereferencing an array in Perl
Consider: 考虑:
sub binary_search {
my ($array, $word) = @_;
my ($low, $high) = (0, @$array -1);
}
I am going through a book and the explanation for @$array
the book provides is that it is dereferencing the scalar variable $array
to get the array underneath. 我正在读一本书,该书提供的
@$array
的解释是,它正在解引用标量变量$array
来获得其下的数组。
I am a bit confused on this statement. 我对此说法有些困惑。 I understand that $ is a scalar variable and @ is an array variable in Perl.
我知道$是Perl中的标量变量,@是数组变量。
my (@array, $word) = @_;
my (@array, $word) = @_;
? $
a scalar?) which can be reached by @$array
? $
标?),它可通过达到@$array
? The function expects a reference to an array, not an array, as the first argument. 该函数希望将对数组而不是数组的引用作为第一个参数。 See perlreftut for info on array references.
有关数组引用的信息,请参见perlreftut 。 If you tried to evaluate:
如果您尝试评估:
my (@array, $word) = @_;
the @array
would gobble up all the input and $word
would be left undefined. @array
将吞噬所有输入,而$word
将保持未定义状态。
The method could have been written to expect a word and then an array, in which case you could do: 该方法可以写成期望有一个单词然后是一个数组,在这种情况下,您可以执行以下操作:
my ($word, @array) = @_;
but that's not what you have. 但这不是你所拥有的。
Note that when you call a function with an array in the actual argument list, what Perl does is interpolate the elements of the array as if they were individual arguments to the function. 请注意,当您在实际参数列表中调用带有数组的函数时,Perl所做的就是对数组的元素进行插值,就好像它们是函数的单独参数一样。 It does not pass the array as a single object.
它不会将数组作为单个对象传递。 See the perlsub documentation for details.
有关详细信息,请参见perlsub文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.