DavidSzczesniak
12/11/2017 - 4:23 PM

Passing multiple arrays to a normal function

In list context, arrays will flatten into lists.

my @cats = qw(Daist Petunia Tuxedo Brad Jack Choco);
my @dogs = qw(Rodney Lucky Rosie);
# Do
take_pets_to_vet(@cats, @dogs);

# Don't do, buggy
sub take_pets_to_vet {
  my (@cats, @dogs) = @_; # arrays are greedy, so @_ will contain 9 elements, not 2
  # after the assignment, @cats will contain every argument passed to the function and @dogs will be empty
  ...
}