Search⌘ K
AI Features

Manipulating Function Arguments

Explore how Perl flattens function arguments and how to manipulate them effectively. Understand dealing with lists, hashes, and scalars in parameters, using experimental signatures, and the implications of aliasing @_ for safer, clearer, and maintainable code.

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:

Perl
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 its value. Hash assignment inside show_pets() works the same way as ...