Perl 子程序数组和标量变量参数 [英] Perl subroutine array and scalar variable parameters
本文介绍了Perl 子程序数组和标量变量参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将标量变量和数组变量同时传递给 Perl 中的子程序?
How exactly can I pass both scalar variables and array variables to a subroutine in Perl?
my $currVal = 1;
my $currValTwo = 1;
my @currArray = ('one','two','three');
my @currArrayTwo =('one','two','three');
&mysub($currVal, $currValTwo,\@currArray, \@currArrayTwo);
sub mysub() {
# That doesn't work for the array as I only get the first element of the array
my($inVal, $inValTwo, @inArray, @inArrayTwo) = @_;
}
推荐答案
您需要获取它们作为引用,因为您已经将它们作为引用传递(通过使用 \
运算符):>
You need to fetch them as references because you've already passed them as references (by using the \
operator):
my($inVal, $inValTwo, $inArray, $inArrayTwo) = @_;
然后使用引用作为数组:
and then use the references as arrays:
@{$inArray}
这篇关于Perl 子程序数组和标量变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文