in reply to Re^7: Scalar context of slice (myth)
in thread Scalar context of slice

Your right that a single operator is created for the entire string of commas. Your snippet doesn't prove that, but -MO=Concise does.

>perl -MO=Concise -wle"print scalar(1,2,3,)" Useless use of a constant in void context at -e line 1. 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 7 <@> print vK ->8 3 <0> pushmark s ->4 - <1> scalar sK/1 ->7 6 <@> list sK ->7 <-- one operator 4 <0> pushmark v ->5 - <0> ex-const v ->- \ - <0> ex-const v ->5 |<- three operands 5 <$> const[IV 3] s ->6 / -e syntax OK

But the list operator doesn't return a list in scalar context. No reduction occurs, default or otherwise.

PP(pp_list) { dVAR; dSP; dMARK; if (GIMME != G_ARRAY) { if (++MARK <= SP) *MARK = *SP; else *MARK = &PL_sv_undef; SP = MARK; } RETURN; }

It's the builtin or operator that makes the choice, not the context.

Replies are listed 'Best First'.
Re^9: Scalar context of slice (myth)
by BrowserUk (Patriarch) on Oct 04, 2008 at 04:35 UTC
    Your snippet doesn't prove that, but -MO=Concise does.

    Damn it! You stole my proof of proof :) I have exactly that on my terminal all ready to c&p.

    But the list operator doesn't return a list in scalar context.

    I never said it did?

    No reduction occurs, default or otherwise.

    If I code:

    sub x{ wantarray ? 'fred' : @_[ 0 .. $#_ ]; } print x( 1, 2, 3 );; fred print scalar( x( 1, 2, 3 ) );; 3

    The list I supplied to x() and attempted to return, appears reduced,; is less (different) than it was; is no longer that same list. That's all I meant by "default reduction".

    Ie. the thing that happens when a construct that would, in a list context, produce a 'list', appears in a scalar context.

    Now, if you can find a better term for that than "list in a scalar context", enlighten away!

    Actually, I think that thing, that I tried to avoid controversy by terming "reduction", can actually be seen happening:

    if (GIMME != G_ARRAY) { if (++MARK <= SP) *MARK = *SP;

    I'd read that (with licence) as: if we're not in an G_ARRAY context, if there are more than one items on the stack, then throw away all but the last item.

    It's the builtin or operator that makes the choice, not the context.

    The builtin or operator? Where? I don't see no or operators?


    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.