Perl Lists

Perl Lists are comma-separated expressions. Parentheses enforce the list context while comma operator creates lists.

List Context in Perl

When parentheses used on the lvalue (left side of the assignment), Perl automatically evaluates the assignment in list context. If both sides of the assignment are lists, the contents of the right list are copied to the one on the left.

Perl Lists vs Container Types

Perl Lists are the basic skeleton that we use to create arrays and hashes. However, there is a difference between lists and arrays/hashes. Arrays/hashes are just containers for a list of values, they can be evaluated in scalar and list contexts.

Perl wantarray Function

Perl wantarray function returns

  • true if the caller expression is evaluated in list context.
  • false if the caller expression is evaluated in scalar context.
  • undef if the caller expression is evaluated in void context.

wantarray function is very helpful when we need to return different values based on the context.

Official documentation of wantarray can be found in CPAN.

Leave a Reply

Your email address will not be published. Required fields are marked *