in reply to passing arrays to asubroutine
Parameters that are passed to a subroutine are evaluated in list context and the resulting list is passed on to the subroutine, and when arrays are evaluated in a list context they are flattened to a list (the same is true of hashes). So when two arrays are passed to a subroutine, they are both evaluated in list context and flattened, resulting in a single list which is then passed onto a subroutine. To get around this we pass a reference to the subroutine instead, and since that is just a scalar it will not change in list context.
Here's some code to demonstrate the above paragraph
So you can see there that the arrays are being flattened, lists are behaving normally and array refs work as expected.use strict; sub foo { print "got: ", join(', ', map { ref($_) ? "[@$_]" : $_ } @_),"\n"; } my @alpha = qw( a b c ); my @nums = qw( 1 2 3 ); # flattened array foo(@alpha); # 2 flattened arrays foo(@alpha, @nums); # array ref and flattened array foo(\@alpha, @nums); # flattened array and 2 array refs foo(@nums, \@alpha, [qw/d e f/]); # flattened array, an array ref and a list foo(@nums, [ qw/4 5 6/ ], qw/7 8 9/); __output__ got: a, b, c got: a, b, c, 1, 2, 3 got: [a b c], 1, 2, 3 got: 1, 2, 3, [a b c], [d e f] got: 1, 2, 3, [4 5 6], 7, 8, 9
_________
broquaint
|
|---|