Search⌘ K

Lists

Explore how Perl handles lists as comma-separated expressions and the difference between lists and arrays. Understand the use of empty lists, scalar and list context, and idiomatic ways to count list elements without temporary variables.

We'll cover the following...

Empty list

When used on the right-hand side of an assignment, the () construct represents an empty list. In scalar context, this evaluates to undef. In list context, it’s an empty list. When used on the left-hand side of an assignment, the () construct imposes list context, hence this idiomEvery language has common patterns of expressions, which include language features and design techniques. These are known as idioms. to count the number of elements returned from an expression in list context without using a temporary variable:

Perl
my $count = () = get_clown_hats();
say $count;
sub get_clown_hats {
return ("flat cap", "bowler hat", "derby hat", "bolo hat", "gat");
}

Because of the right ...