in reply to Re: Scalar Vs. List context
in thread Scalar Vs. List context

array and list elements: these are implicitly in list context.

Not true. Every expression can be evaluated in any context. The context in which the expression is evaluated is determined by the operator to which the expression is an operand.

Expressions that are list elements are no exception. The list operator decides the context of its operands based on its context:

@a = (1,2,3,4); # list, list, list, list $a = (1,2,3,4); # void, void, void, scalar

Expressions that are arrays are no exception.

@b = @a; # @a is evaluated in list context. $c = @a; # @a is evaluated in scalar context. @a; 1; # @a is evaluated in void context.

Replies are listed 'Best First'.
Re^3: Scalar Vs. List context
by jwkrahn (Abbot) on Jul 12, 2009 at 12:52 UTC
    Expressions that are list elements are no exception. The list operator decides the context of its operands based on its context:

    @a = (1,2,3,4); # list, list, list, list
    $a = (1,2,3,4); # void, void, void, scalar

    Your second example does not have any "list elements" because you can't have a list in scalar context.

      Not so. You're considering different definitions of "list" as equivalent.

      A list value can't be evaluated in scalar context. Or any context. They aren't operators, so they can't be evaluated. They exist on the stack.

      The list operator, on the other hand, can be evaluated in any context. Just like every other operator (as far as I know).

      >perl -MO=Concise -e"@a = (1,2,3,4);" ... - <1> ex-list lKP ->8 <-- "l" for list context ...
      >perl -MO=Concise -e"$a = (1,2,3,4);" ... 5 <@> list sKP ->6 <-- "s" for scalar context ...
      >perl -MO=Concise -e"(1,2,3,4); 1" ... 4 <@> list vKP ->5 <-- "v" for void context ...

      A less ambiguous wording of the statement you made is: An operator can't evaluate to (return) a list in scalar context.

      A list operator can be evaluated in scalar context, but it must evaluate to a scalar in scalar context

      Often repeated, but what do you call  ( 1 , 2 , 3 , 4 );? :D
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re^3: Scalar Vs. List context
by ELISHEVA (Prior) on Jul 12, 2009 at 07:35 UTC
    Hmmm.
    use strict; use warnings; sub foo { print "wantarray? ", wantarray?1:0, "\n"; return wantarray? (1,2,3) : 's-s-sc-scalar'; } my @x =('a','b', foo(), 'c'); print "\@x=(@x)\n";

    prints

    wantarray? 1 @x=(a b 1 2 3 c)

    Looks like list context to me.

    Best,beth

      You only showed that it can be evaluated in list context. Why did you ignore my snippet which showed how it can be called in scalar and void context?

      sub foo { print "wantarray? ", wantarray?1:0, "\n"; } my @x =(foo(),foo(), foo()); my $x =(foo(),foo(), foo());
      wantarray? 1 wantarray? 1 wantarray? 1 wantarray? 0 wantarray? 0 wantarray? 0