in reply to Re^6: 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: scalar (1, 2, 3); is the comma operator in scalar context. Of which man perlop says: 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.

Sorry, but that is just (bad) semantics dreamed up (probably long after the fact), to try and explain what actually happens.

Proof: If the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value..., then this should print undef or null or nothing, but it doesn't:

c:\test>perl -wle"print scalar(1,2,3,)" Useless use of a constant in void context at -e line 1. 3
It's the context that makes the choice - not the function.

Okay. I can agree with that. The function can only chose whether to attempt to return a list, or some more meaningful scalar than the default reduction of that list, that the scalar context would otherwise make.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^8: Scalar context of slice (myth)
by ikegami (Patriarch) on Oct 04, 2008 at 03:52 UTC

    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.

      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.
Re^8: Scalar context of slice (myth)
by JavaFan (Canon) on Oct 04, 2008 at 14:38 UTC
    Proof: If the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value..., then this should print undef or null or nothing, but it doesn't:

    You left of binary. The latter comma in

    scalar(1,2,3,)
    doesn't have an operand on its right hand side, so it cannot be a "binary comma".

    In Perl, there isn't something like an "empty statement (or expression)" which would return 'undef'. If there was, then

    sub foo {1;}
    wouldn't return 1, but undef or null or nothing.