in reply to Improper slicing

It's a result of two things:

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Improper slicing
by bobf (Monsignor) on Apr 24, 2006 at 15:01 UTC

    To expand on japhy's post, here is the relevant section from perldata (under "List value constructors"):

    In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element, as with the C comma operator. For example,
    @foo = ('cc', '-E', $bar);
    assigns the entire list value to array @foo, but
    $foo = ('cc', '-E', $bar);
    assigns the value of variable $bar to the scalar variable $foo.

    Update:

    perlop contains information on the comma operator:

    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.

    Thanks to japhy for the reminder.

      It's important to remember that the other operands get evaluated though, in sequence. That doesn't mean anything with ('cc', '-E', $bar), but it does mean something with:
      sub foo { $x = 10 } sub bar { $x = 20 } $x = 30; $y = (foo(), bar(), $x); print "y=$y\n";

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Ah, yes. That helped me find the relevant section in perldata:
      If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator, nor of built-in functions, which return whatever they feel like returning.)

      And it explains why:
      @a=(0..9); @b=(4..6); $a[@b ]= qw/a b c/;
      behaves differently. It's subtle (at least to me). The LIST subscript gets the comma operator applied, but the ARRAY subscript gets the array length applied.