Achilles_Sea has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks:

the following code will print 123456789, leaving @array2 empty.

mysub((1..5),(6..9)); sub mysub{ my (@array1,@array2) = @_; print @array1; }
I wonder why PERL will join the 2 array parameters into array1, and how could I pass several list to a sub routine?

Replies are listed 'Best First'.
Re: why perl joins multy list parameters to one list
by JavaFan (Canon) on Oct 18, 2011 at 11:22 UTC
    You do not have 2 array parameters, you have 2 lists. And the answer is: because Perl is designed to flatten lists - and arrays in list contexts are lists with the array elements. The answer to the second question is: you cannot. You can, however, pass two references to arrays to a sub:
    mysub([1..5],[6..9]); sub mysub { my @array1 = @{$_[0]}; my @array2 = @{$_[1]}; }
      it seems I haven't quite understood the difference between array and list. thank u very much :)
Re: why perl joins multy list parameters to one list
by moritz (Cardinal) on Oct 18, 2011 at 11:34 UTC

    Note that Perl 6 flattens less eagerly, and tries hard to preserve information:

    $ cat flatten.pl sub mysub(@a1, @a2) { say "a1: ", @a1.join(', '); say "a2: ", @a2.join(', '); } mysub((1..5), (6..9)); $ ./perl6 flatten.pl a1: 1, 2, 3, 4, 5 a2: 6, 7, 8, 9