note
Dominus
Says [Ton]:
<blockquote><i>
Is my understanding right? Anything else about lists that I should know about?
</i></blockquote>
It seems to me that there's a major point that you are missing.
<p>
There is a difference between an <i>expression</i> and its
<i>value</i>. An expression is a sequence of characters
in the source code. When the program is run, Perl
evaluates the expression, and the result is a value. A
value is not part of the program source code; it only
exists inside the computer's memory at run time. As a result,
it's hard to talk about value. We say things like this:
<blockquote>
"The value of the expression <tt>(localtime)</tt> is
the list <tt>(36,38,2,2,3,69,3,91,0)</tt>"
</blockquote>
But this is not really correct, because
<tt>(36,38,2,2,3,69,3,91,0)</tt> is <i>not a list</i>.
It is an <i>expression</i> whose <i>value</i> happens
to be a list.
What we really mean here is that
the value of the expression
<tt>(localtime)</tt> is <i>the same as the value of</i>
the expression <tt>(36,38,2,2,3,69,3,91,0)</tt>.<p>
We like to say that the expression
<tt>(36,38,2,2,3,69,3,91,0)</tt> is a <i>list expression</i>,
because it has a certain simple form, and its value is
a list.<p>
Lots of people are confused about this, because it is rarely explained
clearly, and because most people do not use the terminology
consistently. That is why you see beginners asking questions
like this:
<blockquote>
My program reads in a line from a file, adds 1, and prints
it out. The line is: <tt>037</tt>. The manual says
that Perl considers numbers that begin with 0 to be octal constants.
Why did my program print out <tt>38</tt> instead of <tt>040</tt>?
</blockquote>
The answer is that <i>expressions</i> that contain constants
beginning with 0 <i>represent</i> octal constants.
But there is no such thing as an octal <i>value</i>.
Values aren't octal or decimal or binary; they're hidden inside the
computer, and it is None of Your Business how they are
represented. In the beginner's example, Perl is
converting a <i>string value</i> to a numeric value,
and to do that it always interprets the string value as
a decimal numeral.<p>
Now, the thing I think you may have missed about context
is that it applies to expressions, and never to values.
Every time you say "the list on the right-hand side"
you are making a mistake. In scalar context, there is
no list.<p>
<tt>@a</tt> is an <i>array expression</i>. In a
list context, its value is the list of elements from the array.
In scalar context, its value is the length of the array.<p>
<tt>(1,2,3)</tt> is not a list. It is a <i>comma expression</i>.
In list context, its value is a list of the values of
the items separated by the commas.
In scalar context, its value is the value of the last item.
<p>
<tt>@a[0..2]</tt> is not a list. It is not "a.k.a. a list".
It is an array slice expression. In list context,
its value is a list of the array elements you selected.
In scalar context, its value is the value of the last
array element you selected.<p>
<tt>localtime()</tt> is not a list. It is
a <tt>localtime</tt> expression. In list context,
its value is a list of the current seconds, minutes, hours, days, and so on.
In scalar context, its value is a string representing
the current time.<p>
This is why you have to understand that context applies
to expressions, not to values. If you think context applies
to values, you get the wrong answer for <tt>localtime()</tt>.
You would say (as you did in (5) above) "Oh, localtime's value is a list of seconds, minutes, hours,
and so on, and since it's a list in scalar context, the
result is the last element from the list, which is the
<tt>$dst</tt> value." Wrong wrong wrong!<p>
In scalar context, <tt>localtime</tt> doe <i>not</i> produce a list.
<i>No</i> Perl expression produces a list in scalar context.
<p>
Your explanation has a bunch of oddities. In (3)
you talk about Perl assigning one array to another.
But there's only one array there; the <tt>($temp)</tt>
is not an array.<p>
In the section titled <tt>my current understanding</tt>:
<blockquote><i>
2.Arrays and lists act the same in list context. <br>
3.Arrays and lists act very differently in scalar context; the former return their size, and the latter return
their last element.
</i></blockquote>
This is really missing it, because it's impossible to have
a list in scalar context; there is no such thing.
You can have a <i>list expression</i>, but that isn't the
same as an array slice expression, or a localtime expression,
or any other expression whose <i>value</i> might be a list
if it were in list context. But the context effect applies
to the expression itself, not to its value, because
all contextual effects occur at compile time, never
at run time, and at compile time there are no values;
the values do not appear until run time, after all the
contextual effects are resolved.<p>
Perl5-porters got email about a year ago from a guy who
was reporting the following bug:<p>
<pre>
Following is an account of what could be a bug in perl.
$ perl -e 'print "count is ", scalar(undef, undef), "\n"'
count is
Note that count is undefined here, though it should have been 2.
</pre>
This guy had the same idea you did: <tt>(undef, undef)</tt>
is a list of two items, and <tt>scalar()</tt> should count the
items. But <tt>scalar</tt> does no such thing.
It changes the way the way the program is compiled,
so that the comma operator returns its second operand,
instead of constructing a list value.<p>
Hope this helps.<p>
72247
72247