Search⌘ K
AI Features

Context and Array Interpolation

Explore how Perl arrays operate in list context, why arrays flatten when passed to functions, and how to use array references to prevent this. Understand array interpolation in strings and how to customize its separator for clearer debugging in Modern Perl programming.

We'll cover the following...

Arrays and context

In list context, arrays flatten into lists. If we pass multiple arrays to a normal function, they will flatten into a single list:

Perl
my @cats = qw( Daisy Petunia Tuxedo Brad Jack Choco );
my @dogs = qw( Rodney Lucky Rosie );
take_pets_to_vet( @cats, @dogs );
sub take_pets_to_vet {
# BUGGY: do not use!
my (@cats, @dogs) = @_;
say "Cats =>"."@cats";
say "Dogs =>"."@dogs";
}

Within the function, @_ will contain nine elements, not two, because list ...