in reply to Re^10: Scalar context of slice ("list")
in thread Scalar context of slice
Since even that list doesn't return a list, would it be accurate to say "a list can't be returned in scalar context"
Sorry for the delay in getting back to you, but this was one of those things that I had to allow to wash over me for a while before deciding how to respond.
If for no other reason than you believe it is more accurate, then I would have to conclude that it probably is more accurate. And as a generality, most of what you say is accurate. That's (for me) a given.
But the real question is: whom does that greater accuracy benefit?
I thik we can agree, this (1,2,3) is a list; and this return (1,2,3) a return statement; and this
sub x{ return(1,2,3); }
is a function (attempting) to return a list.
If we place a call to that function in a scalar context my $x = x(); then the return statement doesn't cease to be a return statement, nor does the list cease to be a list. At some point, the list is transformed to a scalar, which, in this case, happens to be the last element of the list.
So at that point, immediately before the transformation--and the very reason the transformation is performed--the list is in a scalar context.
Now you could conclude either of:
But whichever way, and to be honest it does matter which is more accurate, the list existed; the scalar context existed and the list was within the auspices of that scalar context; and so, was transformed.
So, I see no clearer, simpler or conceptually more accurate way of describing that than: "the results of a list in a scalar context is the last element of that list".
And this does not apply to map & grep in scalar contexts, not because the description or concept is wrong, but because those functions do not attempt to return a list when they find themselves in a scalar context. They instead choose to return something, a scalar, that is considered more useful.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^12: Scalar context of slice ("list")
by ikegami (Patriarch) on Oct 20, 2008 at 04:22 UTC | |
by BrowserUk (Patriarch) on Oct 20, 2008 at 04:57 UTC | |
by ikegami (Patriarch) on Oct 20, 2008 at 06:47 UTC | |
by ikegami (Patriarch) on Oct 20, 2008 at 06:49 UTC |