|P is for Practical|
Re: dot '.', comma ',' operators "oops" & "huh?"by Abigail-II (Bishop)
|on Aug 22, 2003 at 21:53 UTC||Need Help??|
The comma is still constructing a list, that simply has nowhere to go.
No, sir, it does not. To quote merlyn: There are no lists in scalar context. And to repeat my code fragment:
If the comma would construct a list in scalar context, could you please explain why the code above prints SCALAR twice?
in scalar context, the list constructor creates a list
Repeat after me: There are no lists in scalar context.
The operator isn't overloaded for each possible context.
This is Perl, not C++. Operators aren't overloaded by itself, although they might if one of their operands is an object. But that's not the case we are discussing. The comma operator does, however, behave different in scalar than in list context. man perlop clearly discusses the two cases:
Note how it does not talk about lists when discussing the comma operator in scalar context.
It's operating on the list, but the list's subjects aren't being assigned except for the last one, because there's only one slot for them to fit into.
Then explain this warning:
Note how it says void context. Make sure that your explaination also explains why the following doesn't give a warning:
Here's an example:
That both sides are being evaluated isn't what's being discussed. The comma in scalar context evaluates both operands, and that's what we are seeing here. But evaluating both operands does not make a list - addition evaluates both operands as well, and that doesn't create a list either.
That's undefined behaviour. Perhaps you want something like:
Note that the comma operator is now in list context, and that $a, $b and $c are assigned to the first three elements of the list.
I guess my point to all this is that the commas construct lists in both list and scalar context,
There's no list in scalar context.
but a list is only returned in scalar context.???