我想获取在函数中发送的数组,但它似乎是空的。我send_file();
在参数中使用数组进行调用
send_file($addr, @curfile);
这就是我找回参数的方式
sub send_file($$)
{
my $addr = $_[0];
my @elem = @_;
...
}
为什么my @elem
是空的?我如何才能在不丢失所有内容的情况下取回阵列?
您应该改为通过引用传递数组:
#!/usr/bin/perl
use strict;
use warnings;
my $test_scalar = 10;
my @test_array = qw(this is a test);
sub test($\@)
{
my ($scalar, $array) = @_;
print "SCALAR = $scalar\n";
print "ARRAY = @$array\n";
}
test($test_scalar, @test_array);
system 'pause';
输出:
SCALAR = 10
ARRAY = this is a test
Press any key to continue . . .
如果您想做同样的事情而不通过引用传递,请将$$更改为$ @并使用shift,这样第一个参数就不会包含在数组中。不过,通过引用传递数组是更好的编码实践。。。这只是向您展示如何在不通过引用的情况下完成它:
#!/usr/bin/perl
use strict;
use warnings;
my $test_scalar = 10;
my @test_array = qw(this is a test);
sub test($@)
{
my ($scalar, @array) = @_;
print "SCALAR = $scalar\n";
print "ARRAY = @array\n";
}
test($test_scalar, @test_array);
system 'pause';
这将为您提供相同的输出。
如果您确实不需要$ @,也可以完全取消$ @。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句