Ben Morrow said:
But that would be quite wrong. In Perl, a function *cannot* return
an array, only a list if in list context or a scalar if in scalar.
That's true, of course. I think I got it right earlier: f() returns
the result of evaluating @INC in scalar context when it's called in
scalar context, and the result of evaluating @INC in list context when
it's called in list context. Condensing this to "f() returns @INC" is
less precise, but maybe it's close enough to be useful, assuming that
we all know you can't *really* return an array from a subroutine and
do "push foo(), $elt" or whatever.
For instance, what about this
% perl -le'sub f { caller } print scalar f'
? Would you say this returns 'the caller function', which is then
evaluated in scalar context?
Well, no. That would be silly.
It certainly doesn't return a list:
It certainly doesn't!
according to you, the result of 'evaluating a list in scalar context'
is its last element, which is not what is returned.
That's because (as you must know) caller() doesn't return a list in
scalar context. Since there's no list, this doesn't have anything
to do with the behavior of a list in scalar context.
In fact, as Randal keeps saying, there is *no such thing* as 'a list
in scalar context'.
That's a simplification, too. It's probably the best thing to tell
people who are trying to figure out what a built-in does in scalar
context based on what it does in list context. But it's not the whole
story -- perldata explains this more accurately:
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.)
OK, the comma operator in scalar context builds a list and then
discards all but the last element. Just out of interest (my perl
isn't built with DEBUGGING), what does this do
% perl -Dts -le'scalar (1, 2, 3)'
? Is the whole list built, or are the values discarded as it goes
along?
Neither.
Here the compiler knows that the potential list is in scalar context,
and it puts the "1" and "2" in void context, since they're just going
to be discarded. Constants in void context can be optimized away
(i.e. discarded at compile-time) and that's what happens here.
This is the zen-like "There Is No List..." scenario from the FAQ:
As a side note, there's no such thing as a list in scalar con-
text. When you say
$scalar = (2, 5, 7, 9);
you're using the comma operator in scalar context, so it uses
the scalar comma operator. There never was a list there at
all! This causes the last value to be returned: 9.
And perlfunc:
A named array in scalar context is quite different from what would
at first glance appear to be a list in scalar context. You can't
get a list like "(1,2,3)" into being in scalar context, because the
compiler knows the context at compile time. It would generate the
scalar comma operator there, not the list construction version of
the comma. That means it was never a list to start with.
And they're correct, more or less, for the examples they give. But the
bit about the "scalar comma operator" seems to be misguided. If any of
the earlier elements of those lists were variables, a list really would
be created at runtime.
% perl -Dts -le 'scalar($$, 42, 43)'
EXECUTING...
=>
(-e:0) enter
=>
(-e:0) nextstate
=>
(-e:1) pushmark
=> *
Here's the list (note the missing "42").
(-e:1) gvsv(main::$)
=> * IV(14592)
(-e:1) const(IV(43))
=> * IV(14592) IV(43)
Here's the op that evaluates the list in scalar context.
(-e:1) list
And here's the result.
=> IV(43)
(-e:1) leave