Perl – Understand List, Unary operation and Array in scalar context

The common misunderstanding in Perl is the difference between :

  • A list : (1, 2, 3)
  • An array : @my_array containing (1, 2, 3)
  • The unary operation coma ‘,’: $operation1, $operation2, $operation3

Perl use the context to act practically over logically. That lead to unexpected behavior if you mislead each of this concept.

The 3 contexts are : void context, list context, scalar context. We will focus on the scalar context here.

First of all, what is the difference between a list and an array, and when the one and the other is used.

When you declare an array :

You can use several function like ‘push’, ‘scalar’ … on it.

You cannot assigned a list to a variable, a list is ephemeral and not operation can be done on it.

You cannot do :

It has no meaning. the push (1,2,3), 4 is equivalent to push 1,2,3,4, so push try to push 2, 3, 4 into the constant 1. And that doesn’t work.

Now let show different behavior between all of this concept :

The unary operator :

Here we have the unary operation coma ‘,’.

To write it more clearly we can do :

So $x = 4 in that case.

The list :

We use a list in a scalar context. In that case the list will return the last element.

So $x = 6;

The array :

We use an array in a scalar context. In that case the array will return the size of the array.

So $x = 3

Now a bit more tricky, the mix of it :

The operation assign the array to $x, in that case that mean the size of the array, then execute the constant 7.

So $x = 3

The array in a list context will expand into a list with ’4, 5, 6′ and then we add 7. That create a list with 4, 5, 6, 7.
A list will return the last element in a scalar context.

So $x = 7;

And take care of the unary operator against the list :

Here we don’t have a list assign to an array, but the unary operator.

So @x = (4).

If you use a list :

Each element of the list is added to the array.

So @x = (4, 5, 6).

If we are in a function, the behavior is different, always for the practical other logical.

The unary operation is transformed into a list before the function return it’s result.
When we mix an array and a list, it result into a list, then the latest result is returned in scalar context.
When we return an array in a scalar context, the result is the size of the array.

We can replace the array return with :

We cannot generalize the behavior of a function by assuming that a method always return a list except when we return an array.

Each method can have access to the context when we call it and act differently. We often want a function return the most practical result.

The only way to know how a function behave, it to read the documentation on it.

I will show you an example with the “sort_in_place” function :

This method check the context. The “wantarray” (badly named), can be used to get the context.
Here the value based on context :

  • equal “1″ in a list context.
  • equal empty string “” in a scalar context. So wantarray() is false but defined wantarray() is true.
  • equal to undef in a void context. So wantarray() and defined wantarray() is false.

So :

In list context :

In scalar context :

In void context :

To resume, you have to take care of how you use the context in Perl, and never assume based on your experience that a function behave in a certain way because the array or list behave the same way. You should always check the documentation of the function you use to know how this one behave on different context.

If you have question, or critic, fill free to comment. I will gladly improve this document based on your comments.

I hope it is more clear for you to understand the strange behavior of some functions now.



Short URL:

2 thoughts on “Perl – Understand List, Unary operation and Array in scalar context”

  1. You’ve almost got it, but there are several problems.

    * You can assign a list to a variable (array or hash). I’m not sure you meant to say that you can’t.
    * [1,2,3] is an array reference (a scalar), not an array variable
    * defined wantarray is scalar context when wantarray is false. That’s not emphasized in your list.

    1. Thanks Brian.
      I have fix the pod doc. So instead of [1,2,3], I have say @my_array containing (1,2,3)
      I have add “defined wantarray() is for scalar context only if wantarray() is false.

      About the assign to a variable, I have only cover the scalar context. Of course
      @x = (1,2,3) will push into @x the value 1,2,3

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">