Search⌘ K

Implicit Ideas: The Default Array Variables

Explore how Perl manages function arguments with the implicit @_ array and handles command-line inputs using the @ARGV array. Understand how built-in functions operate on these arrays by default and how this behavior supports writing concise and flexible Perl programs.

Function arguments array: @_

Perl provides two implicit array variables. Perl passes arguments to functions in an array named @_. Array operations inside functions use this array by default. These two snippets of code are equivalent:

Code_1
Code_2
sub foo {
my $arg = shift;
...
}

Just as $_ corresponds to the pronoun “it,” @_ corresponds to the pronouns “they” and “them.” Unlike $_, each function has a separate copy of @_. The built-ins shift and pop operate on @_, if provided no explicit operands.

Command-line arguments array:

...