Search⌘ K
AI Features

Function Parameters

Explore how Perl functions accept and manipulate parameters through the array @_ , using techniques like shift, list assignment, and array operations. Understand best practices for accessing single or multiple arguments and passing leftover parameters with clarity and efficiency.

The array of function parameters

A function receives its parameters in a single array, @_. Perl flattens all provided arguments into a single list when we invoke a function. The function must either unpack its parameters into variables or operate on @_ directly:

Perl
sub greet_one {
my ($name) = @_;
say "Hello, $name!";
}
sub greet_all {
say "Hello, $_!" for @_;
}
greet_one('Sam', 'Ash');
greet_all('Sam', 'Ash');

Operations on function parameters

@_ ...