http://www.perlmonks.org?node_id=663493


in reply to Re: Scalars, Lists, and Arrays
in thread Scalars, Lists, and Arrays

Hrm, I just noticed a clear mistake in your node.

But scalar does no such thing. It changes the way [...] the program is compiled, so that the comma operator returns its second operand, instead of constructing a list value.

No, the comma operator is actually compiled exactly the same way. What changes at compile time is that a scalar op-node is added which changes what the other op-nodes do at run time, preventing them from building a list of scalar values on the stack (well, for most operations that can produce a list of scalar values on the stack, but not for all of them, slices being the most obvious exception).

But the context effect applies to the expression itself, not to its value, because all contextual effects occur at compile time, never at run time, and at compile time there are no values; the values do not appear until run time, after all the contextual effects are resolved.

No, all contextual effects certainly cannot occur at compile time. A simple counter-example is:

sub contextSensitive { (foo(),bar()) } contextSensitive(); my $s= contextSensitive(); my @a= contextSensitive();

That code doesn't compile two or three different versions of the contextSensitive() subroutine, one for each possible context. It compiles the (foo(),bar()) code exactly one way and at run time the behavior is changed depending on what context the sub was used in.

In fact, my testing shows that most contextual effects occur at run time. Any contextual effects happening at compile time would be optimizations requiring extra code so I'm not surprised that I couldn't find any cases of that. It would also require some static analysis of the parsed code to even determine if the context can be known at compile time. This type of static analysis is pretty rare in perl.

- tye        

Replies are listed 'Best First'.
Re^3: Scalars, Lists, and Arrays (run time)
by Dominus (Parson) on Jan 23, 2008 at 15:21 UTC
    > No, the comma operator is actually compiled exactly the same way.
    Please reread the sentence you quoted. I did not say the comma operator is compiled differently. I said the scalar operator changes the way the program is compiled. And it does change the way the program is compiled, by causing the insertion of an additional op node into the optree.

    Then I said that the change to the way the program was compiled causes the comma operator to return its second operand, instead of constructing a list value. As you explained, the additional op node has precisely this effect.

      I could buy that weaseling out if you had said those things in the order in which I had quoted them. But the first thing you said about compile/run time was:

      because all contextual effects occur at compile time, never at run time

      So forget my complaint about your subsequenty statement (which I find misleading but can be awkwardly interpretted as "correct" as you note, especially if you ignore the above phrase).

      You could claim that contexts are created at compile time but the effects of those contexts clearly must at least sometimes be applied at run time (since contexts get passed in to subroutines) and, in current versions of Perl, most if not all contextual effects occur at run time (nearly the opposite of what you claimed).

      - tye        

Re^3: Scalars, Lists, and Arrays (run time)
by ysth (Canon) on Feb 09, 2010 at 03:30 UTC
    Any contextual effects happening at compile time would be optimizations requiring extra code so I'm not surprised that I couldn't find any cases of that. It would also require some static analysis of the parsed code to even determine if the context can be known at compile time. This type of static analysis is pretty rare in perl.
    But that analysis is exactly what happens - the syntax tree is descended and every operation applies context (or doesn't) to its operands, marking then as having list, scalar, or void context as determined by the operation. Then anything left unmarked gets context from the call stack at runtime.
    --
    A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
    Online Fortune Cookie Search
    Office Space merchandise