### Re^9: If you believe in Lists in Scalar Context, Clap your Hands

by ikegami (Pope)
 on Oct 24, 2008 at 20:03 UTC ( #719426=note: print w/replies, xml ) Need Help??

If you can't have a list in a scalar context, how can you have the assignment of a list in scalar context? You have even said in this thread that lists don't even exist.

The list assignment imposes a list context on its operands. So while the list assignment is in scalar context, the assignee and assigned lists aren't.

```   A     B      C     D        <-- labels
\$x = ( \$i, \$j ) = ( \$p, \$q );
S  ?   L L L    S   L L L      <-- context
|  |   | | |    |   | |  \__ Propogated by right-most list op'tor (D).
|  |   | | |    |   |  \____ Imposed by the list assignment (C).
|  |   | | |    |    \______ Propogated by right-most list op'tor (D).
|  |   | | |     \__________ Imposed by the scalar assignment (A).
|  |   | |  \_______________ Propogated by left-most list op'tor (B).
|  |   | \__________________ Imposed by the list assignment (C).
|  |    \___________________ Propogated by left-most list op'tor (B).
|   \_______________________ Determined by outside code.
\__________________________ Imposed by scalar assignment (A).

Replies are listed 'Best First'.
Re^10: If you believe in Lists in Scalar Context, Clap your Hands
by BrowserUk (Pope) on Oct 24, 2008 at 20:14 UTC

Now try and replace all the information conveyed by the use of the word 'list' in the following sentence from perlsub, with the one or two dozen diagrams like the above, it would take to replace that one word.

If you return one or more aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list.

The only purpose served by the "there's no such thing as a list in a scalar context" meme, is the promotion of one person's intimate knowledge of perl's internals over another's lack thereof; or lack of interest in same.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The quote is true. Of course, the only way to return more than one aggregate is to be in list context.

If a flattened list was being returned in the following code, it would print "d" instead of "4".

```perl -le"print scalar sub { my @a = qw(a b c d); return (@a,@a) }->();
+"

The only purpose served by the "there's no such thing as a list in a scalar context" meme

So? I'm against it too. Too many definitions of list involved. I've been promoting "an operator/function/sub/expression can't return a list in scalar context".

The quote is true. Of course, ... If ... it would ... sub ...

Believe it or not, I don't need you to explain these things. I know what happens, but that is a side issue to the point the OP was making in his meditation. As it was in thread that preceded it.

It's not about what actually happens, it about simple terminology for describing (or analogising) what appears to happen.

I've been promoting "an operator/function/sub/expression can't return a list in scalar context".

But that is no replacement for the phrase "a list in scalar context". It's a statement about what doesn't happen, not about what does.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re^10: If you believe in Lists in Scalar Context, Clap your Hands
by mr_mischief (Monsignor) on Oct 24, 2008 at 21:16 UTC
Thanks for the diagram, but it does not answer my point, let alone refute it. Of the two sentences you quoted, you only attempt to respond to one as far as I can tell.

Saying "the context of the assignment operator is scalar, which changes what particular representation of the list is propagated" does nothing to show that there's no context applied to the list. Saying the values propagate from right to left through the contexts is equivalent to saying that the contexts propagate from left to right through the values.

Now, about that second sentence you quoted... If there is no list, how can there be a list assignment? In what language are you writing that looks so much like English but in which "list assignment" does not mean "assignment of a list"?

What does "So while the list assignment is in scalar context, the assignee and assigned lists aren't." if the "list" doesn't exist?

What the heck is "list op'tor"? Does that stand for "list operator"? Upon what does a "list operator" operate?

Show me the list assignment that is in scalar context here:

```\$ perl -E 'say scalar(() = (\$a, \$b, \$c))'
|      |
\      \_____ is a list?
\
\__________ enforces scalar context?

?_______ second list assignment?

Is there a list there? Is there a set of scalar values context assignment operator present other than the one between the empty set of scalar values and the set of three scalar values? In what context is the empty set of scalar values?

I don't believe you've shown there's no concept of a list being coerced into a scalar context. I especially don't believe you've shown there's no concept of a list in Perl.

Of the two sentences you quoted, you only attempt to respond to one as far as I can tell.

The inclusion of the second sentence was accidental. I hadn't even read it. If JavaFan said that lists don't even exist, that I don't agree with him. I suspect that's not what he meant, though. I can't address this question for him.

Saying [...] does nothing to show that there's no context applied to the list.

I agree. Nothing will show that because everything is evaluated in some context.

Saying the values propagate from right to left through the contexts is equivalent to saying that the contexts propagate from left to right through the values.

Operands are evaluated in a context chosen by their operator. Precedence and associativity determine what is an operand to what operator, but doesn't otherwise affect context.

To clarify, by "an operator propagating a context onto an operand", I mean "an operator choosing to impose the same context upon an operand as the context imposed upon the operator itself".

What does "So while the list assignment is in scalar context, the assignee and assigned lists aren't." if the "list" doesn't exist?

Where did you get that from? I didn't say some list didn't exist. Quite the opposite, I showed two list operators in list context, so two lists were created.

What the heck is "list op'tor"? Does that stand for "list operator"?

Yup. I wanted to avoid reaching the default wrap length for code blocks.

() -- is a list?

Yes, an empty list literal.

scalar -- enforces scalar context?

Yes, onto its operand, the list assignement.

Show me the list assignment that is in scalar context here:

```                       |
v
perl -E 'say scalar(() = (\$a, \$b, \$c))'
V   L      L  S  L \ L/  L
\/
L

Is there a list there?

Multiple.

• () (or rather the nothing inside the parens) is a list operator.
• ,, is a list operator (or two depending if you think of it as a binary operator).

• () returns a list of zero scalars.
• (\$a, \$b, \$c) returns a list of three scalars.
• scalar(...) returns a list of one scalar.

Is there a set of scalar values context assignment operator present other than the one between the empty set of scalar values and the set of three scalar values?

What's a "scalar values context assignment operator"?

In what context is the empty set of scalar values?

You mean the ()? List context. The () is the LHS of a list assignment operator, and a list assignment operator imposes a list context on its LHS operand. (On its RHS operand too.)

If JavaFan said that lists don't even exist, that I don't agree with him. I suspect that's not what he meant, though.
What I said is that Perl doesn't know about lists. A list is not a Perl datastructure. Lists don't have names. Once the program is compiled, "lists" are just a bunch of scalars on the stack. In the same way that Perl doesn't really know about string literals. Once the program is compiled, a string literal is a value. Note also that I made the remark in response to a request for clearification about the difference arrays and lists. Arrays are first class objects. They (can) have names. They have their own datastructure. They can be referenced. Lists have none of that; they aren't even second class objects.

That of course doesn't mean that humans can't talk about lists. Just as humans can talk about object attributes - Perl wouldn't know what an object attribute is.

The result of the whole expression is what I'd say is in scalar context, actually. Assess the context here:
```perl -e 'print scalar( (\$x, \$y ) = ( 42, 101, 202 ) ); print "\n\$x\n\$y
+\n";'
The assignment operator itself happens to be both in scalar and in list context. It produces both a list for the list, and a scalar for the function scalar(). Which oddly leads to the possibility of saying the assignment operator in this:
```perl -E 'say scalar(() = (\$a, \$b, \$c))'
... is both in scalar and void contexts.

I'm not sure how this is any more clear than saying there are exceptions to a general rule about lists returning something of their own accord.

Create A New User
Node Status?
node history
Node Type: note [id://719426]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-03-25 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (299 votes). Check out past polls.

Notices?