Manipulating Function Arguments

Learn how to manipulate function arguments.

We'll cover the following...

Flattening

List flattening into @_ happens on the caller side of a function call. Passing a hash as an argument produces a list of key/value pairs:

Press + to interact
my %pet_names_and_types = (
Lucky => 'dog',
Rodney => 'dog',
Tuxedo => 'cat',
Petunia => 'cat',
Rosie => 'dog',
);
show_pets( %pet_names_and_types );
sub show_pets {
my %pets = @_;
while (my ($name, $type) = each %pets) {
say "$name is a $type";
}
}

When Perl flattens %pet_names_and_types into a list, the order of the key/value pairs from the hash will vary, but the list will always contain a key, immediately followed by ...