in reply to Unexpected variable assignment

An array in scalar context returns its size. A list in scalar context returns the last element. Your code is dealing with a list.

The best way I've found to distinguish between the two is that if you're manipulating the named thing, you're working with an array. Otherwise, chances are you're working with a list.

my @x = (6 .. 10); my $a = (6 .. 10); my $b = @x; print "$a $b\n";

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Unexpected variable assignment
by jonnyfolk (Vicar) on Apr 09, 2003 at 22:31 UTC
    Sorry, I'm being obtuse, here.

    Is it a list because an operation occurs on an element within ()?. When I looked at it saw an array in scalar context. Your explanation hasn't quite come down to my level of incompetence - fancy another go? :)

      The parenthesis don't construct a list. In this case, the context of the assignment does.

      my $scalar = 1, 2, 3; # interpreted as: (my $scalar = 1), 2, 3; my @letters = 'a', 'b', 'c'; # interpreted as: (my @letters = 'a'), 'b', 'c';

      The problem here is that the precendence of the assignment is higher than the precendence of the comma. The paranthesis group the constants and help the parser realize that the comma is not separating statements.

      Arrays are accessed via containers (variables). Lists aren't.

      Arrays may or may not have names -- anonymous arrays are still arrays.

      Lists can be stored in arrays, but then they cease to behave as lists.

        Thanks for the explanation, chromatic - that's cracked it!
      Not, not really. A list isn't a Perl data structure, but an array is. A list is just a bunch of stuff. The way we work with "stuff" in Perl is to use the "comma operator" (and its cousin, '=>'). The comma operator is one of those things that no-one (who isn't on p5p) thinks about and expects to just DWIM. But, it's the way that arrays and lists interacting possible.

      Think about it this way - a list is some stuff collected together, but an array is a place in memory with all sorts of magic associated with it. This means that you can take an array and do useful operations on it, but you cannot do that with a list. However, there is no such thing as "array context" - it's "list context".

      The reason why most people confuse the two (and why they're usually interchangeable) is that an array will (usually) impose list context around it, if it's an lvalue. (Also, there's the deplorably-named wantarray, which actually checks for list context and has nothing to do with arrays.)

      At this point, you've exhausted my knowledge. If you want more, I suggest reading the relevant portions of the Camel book. I did so at one point, thought it was neat, then promptly forgot it cause it's never once been relevant to me in my years of Perl development. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.