Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Going one step further than the discussion below Chas's answer, there's no such thing as a list period.</p> <p>Unlike Python which has the tuple type filling this role, Perl's lists are entirely a construct of the stacks in the compiler/interpreter. This is why you can not take a reference to a list. </p> <p>Just like python's use of tuples, perl automatically pushes and shifts its stack to move around groups of variables when a construct such as the <code>=</code> assignment operator is used. (Perl's precedence rules are what require the parenthesis, since <code>=</code> binds tighter than <code>,</code>). The macro level difference between Perl's and Python's mechanisms is that context propagates through the entire expression.</p> <p>So when in scalar context, that context is distributed to each of the <code>,</code> operators and the operator executes its left side in void context and its right side in scalar context which it then returns. However in list context the <code>,</code> operator pushes its arguments onto the stack, all of which are executed in list context. The stack is then passed across the assignment operator and then shifted into the lvalue. The stack/list itself is immutable to Perl level code, it's modification tools are effectively all of Perl's syntax. </p> <p>So while it is incorrect to refer to a list in scalar context (because such a thing can not happen), you can refer to the behavior of list like syntactic constructs (constructed by the <code>,</code> operator, constructed by an array slice or list slice). In scalar context, the behavior of list like constructs is to return the last element of the list. How this is achieved is up to the operators in the expression. this is of course in contrast to the behavior of arrays in scalar context which return their length.</p> <p>To clarify with an example and finally obey the rules and actually answer the question:</p> <p>Assuming <code>my @array = (10, 11, 12);</code></p> <ul> <li><p>scalar context</p> <pre><code>my $val = (10, 11, 12); # $val is 12 my $val = (10 .. 20)[0 .. 5]; # $val is 15 my $val = @array; # $val is 3 my $val = function(); # $val is the last executed expression </code></pre> <ul> <li>so if <code>function</code> ends with <code>10, 11, 12</code> then the value will be 12</li> <li>if it instead is <code>@array</code> then the value will be 3</li> <li>and if it ends with <code>10, 11, 12, @array</code> the value is also 3 <br /> <br /></li> </ul></li> <li><p>list context</p> <pre><code>my ($val) = (10, 11, 12); # $val is 10 my ($val) = (10 .. 20)[0 .. 5]; # $val is 10 my ($val) = @array; # $val is 10 my ($val) = function(); # $val is the first executed expression # in the last statement. </code></pre> <ul> <li>so if <code>function</code> ends with <code>10, 11, 12</code> then the value will be 10</li> <li>if it instead is <code>@array</code> then the value will be 10</li> <li>and if it ends with <code>10, 11, 12, @array</code> the value is also 10 <br /> <br /></li> </ul></li> <li><p>and for completeness, list context grabbing the whole list into an array</p> <pre><code>my @val = (10, 11, 12); # @val is 10, 11, 12 my @val = (10 .. 20)[0 .. 5]; # @val is 10, 11, 12, 13, 14, 15 my @val = @array; # @val is 10, 11, 12 my @val = function(); # @val is the the last executed statement </code></pre> <ul> <li>so if <code>function</code> ends with <code>10, 11, 12</code> then <code>@val</code> will be 10, 11, 12</li> <li>if it instead is <code>@array</code> then <code>@val</code> will be 10, 11, 12</li> <li>and if it ends with <code>10, 11, 12, @array</code> then <code>@val</code> will be 10, 11, 12, 10, 11, 12</li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload