in reply to difference between array and list in perl

Calling a list a speial type of array is a little misleading. An array is a variable, a list is not. In your example:
@list1 = (1,2,3,4); @list2 = (list1, list2, list3, list4); @arr1 = ("1" , "2", "3", "4"); @arr2 = ("arr1", "arr2", "arr3", "arr4") ;
@list1, @list2, @arr1, and @arr2, are all arrays. In all your examples the right hand side of the assignments are lists, although the list (list1, list2, list3, list4) is questionable, since it contains bare words which might be interpreteted as subroutine calls. To ensure text you can autoquote each word using qw, for example:
my @arr3 = qw(value1 value2 value3 value4);
qw returns a list (in an old release it used to return an array, and that was considered to be a bug).

Fortunately you can assign the items in a list to an array, as you have. When we do this kind of assignment it is essentially a copy of the items on the right hand side into elements in the array.

You can assign to a list on the left side, provided the list only consists of variables (although undef is a special case). For example:
my @arr5 = qw (quick brown fox); my ($speed, $colour, $animal) = @arr5;
will assign the three values to the array, then assign from the array into the three variables $speed, $colour, $animal arranged in a list.

Replies are listed 'Best First'.
Re^2: difference between array and list in perl
by JavaFan (Canon) on Jan 16, 2010 at 22:42 UTC
    although the list (list1, list2, list3, list4) is questionable, since it contains bare words which might be interpreteted as subroutine calls
    Whether they are subroutine calls or not doesn't matter. It's a list because of the context - the RHS of a list assignment is always a list. The fact the assignment is a list assignment is caused by the LHS being an array.
    qw returns a list
    No, it doesn't. In Perl, it's not the operation (operator, subroutine, function) that determines whether a list or a scalar is returned - it's the context. It's always the context. qw in scalar context cannot return a list - there are no lists in scalar context.1
    in an old release it used to return an array
    In which release was that? And what does "returning an array" mean? Could I push on it? Slice from it? Did it have a name? Perhaps you're confused by the fact in earlier releases qw was calculated at run-time (by doing a split), and nowadays qw is calculated at compile time.
    You can assign to a list on the left side, provided the list only consists of variables....
    ...then assign from the array into the three variables $speed, $colour, $animal arranged in a list.
    You're contradiction yourself. First you say it assigns to a list, then you say it assigns to a three variables. It's actually the latter that happens.

    1Who will be the first one to claim this time there's such a thing as lists in scalar context?

      Who will be the first one to claim this time there's such a thing as lists in scalar context?

      There is. Here's one:

      >perl -MO=Concise -e"$x = (4,5,6)" 2>&1 | find "list" 5 <@> list sKP ->6 ^ ^ | | list scalar context

      That's why it's clearer to say "an expression cannot return a list in scalar context". In the above example, the list evaluates to the scalar 6.

      Since you know better, why do you use such a needlessly confusing sentence?

      > there are no lists in scalar context

      This is - as usual - a definition conflict!

      As I already pointed out there are (at least) two widespread interpretations of what "LIST" mean:

    • a literal comma separated sequence of expressions visible in source code
    • the realization for passing theses values at run time through an internal stack

      Take this example code:

       perl -e 'print scalar ($a++,"b","c"), $a'

      Following your explanation one might think that $a should never be incremented, since the "list" (the stack) is never built!

      But actually one gets c1 as output, since the "list" (the sequence) is evaluated step by step

      Cheers Rolf

      UPDATE: shortened post

        the realization for passing theses values at run time through an internal stack
        Don't confuse implementation (perl) with the language (Perl)! Otherwise, you'd have to start claiming Perl has pointers!
        perl -e 'print scalar ($a++,"b","c"), $a'

        Following your explanation one might think that $a should never be incremented, since the "list" (the stack) is never built!

        Bollocks! What you have here is the scalar comma operator. From perlop:
        Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C’s comma operator.