更新时间:2023-11-12 09:46:22
因为当您将参数传入或传出子例程时,所有哈希和数组都会被粉碎.
Because when you pass arguments into or out of a subroutine, any hashes and arrays are smashed flat.
您正在分配到%b
,它将吞噬所有参数.
You are assigning into %b
which will gobble up any arguments.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub test1 {
my ( $first, @rest, $last ) = @_;
print Dumper \@rest;
print "First = $first, last = $last, rest = @rest\n";
}
sub test2 {
my ( $first, $second ) = @_;
print "@$first ; @$second";
}
test1 ( 1, 2, 3, 4 );
test2 ( [1,2], [ 3,4] );
my @list1 = ( 1,2,3,4 );
my @list2 = ( 5,6,7,8 );
test1 ( @list1, @list2 );
test2 ( \@list1, \@list2 );
如果要保持数组或哈希完整无缺,则需要通过引用或作为最后一个参数传递它们.
If you want to keep arrays or hashes intact, you need to either pass them by reference or as the last argument.
如果在此处打开strict
和warnings
,也可能会收到警告-这是强烈建议使用的原因之一-因为$b
和%b
不同.您还会收到有关作业数量奇数的警告:
You would also probably get a warning if you turned on strict
and warnings
here - which is one of the reasons it's strongly recommended - because $b
and %b
are not the same. You'd also get a warning about an odd number of assignments:
Odd number of elements in hash assignment at line 5.
Use of uninitialized value $b in print