in reply to Re^5: Scalar context of slice (myth)
in thread Scalar context of slice
No. There is no such thing as a list in scalar context. Lists can only exist in list context. What you have here:
is the comma operator in scalar context. Of which man perlop says:scalar (1, 2, 3);
Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator.except, when they find their return context is SCALAR they chose not to return a list, but instead they return a scalar
A feat not reserved for map and grep, but shared by every operator and function in Perl, including, without a single exception, all user defined functions. Anything that is in non-void scalar context will return exactly one value. And nothing will ever be returned in void context.
It mostly a matter of words, but I think it is wrong to say "map and grep each return lists", when like any other perl function they can chose whether to return a list, a scalar or nothing at all depending upon the context in which they are called.
You are mostly right. Except that a function or operator cannot chose whether to return a list, a scalar or nothing at all. It's the context that makes the choice - not the function.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Scalar context of slice (myth)
by BrowserUk (Patriarch) on Oct 04, 2008 at 02:22 UTC | |
by ikegami (Patriarch) on Oct 04, 2008 at 03:52 UTC | |
by BrowserUk (Patriarch) on Oct 04, 2008 at 04:35 UTC | |
by JavaFan (Canon) on Oct 04, 2008 at 14:38 UTC |