From: www.itworld.com

Context is Everything

by Andrew Johnson

March 1, 2001 —

 

When writing Perl programs, you really must be aware of the two main
contexts: Scalar and List. Things can behave differently when evaluated
in one or the other context.

We'll employ probably the most well know example, assigning an array:

my @list = @array; # @array is in list context
my $scalar = @array; # @array is in scalar context

The first case above assigns @array to another array, providing list
context, so it returns the list of its contents. The second example
assigns @array to a scalar variable, thus putting it in scalar context.
An array in scalar context returns the size of the array. Now, one
twist on the second example places @array in list context.

my ($scalar) = @array; # @array is in list context;

The parentheses around the scalar indicates a list of scalars (in this
case, a list of length one) on the assignment's left side, thus putting
the @array in list context. Now @array returns the list of its contents
and the first element is assigned to $scalar (the rest of the list
returned by the array is ignored).

Another example uses the keys() function of a hash. In list context, it
returns the given hash's list of keys; but in scalar context, it
returns the number of keys in the hash. A hash itself in list context
returns the full list of key value pairs (in the same sequence as the
keys() function returns the keys). However, in scalar context it
returns information about the underlying hash structure.

my %hash = (one => 1, two => 2);
print %hash, "\n"; # prints: one1two2
print scalar %hash, "\n"; # prints: 2/8

The print() function provides a list context (the function expects to
receive a list of arguments). The scalar() function can be used to
explicitly evaluate an expression in a scalar context (as we do in the
last example above). That final version tells us that our hash
currently has 8 buckets allocated and that two are being used.

Now we've already seen that functions can return different results
depending on context with the keys() function, so let's look at the
localtime() function (see: perldoc -f localtime). Operators can also
act context dependently, as with the match operator m// (and that
behaviour is also modified by the /g modifier). The x operator (string
replication) works differently if its left side is a scalar or a list.

Functions themselves may also supply context to their arguments. Take,
for example, the join() function. It first expects a scalar value as an
argument holding the string to be used as the join separator. If you
thought you could just put all the arguments to join into a single
array and pass that, then you wouldn't get the results you wanted.

my @args = (":", 1,2,3);
my $str = join @args;
print $str; # $str is empty

Above, the join() function expected a scalar as the first argument and
evaluated the @args array in a scalar context to get the separator
character (in this case the size of the array which is 4). No arguments
followed to join together.

If a function or operator behaves differently depending on scalar or
list context, it is documented in the relevant documentation pages (see
the perlfunc and perlop manpages).

Next Week: More on Context (boolean and operator contexts)