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

Seems to me that a list in a scalar context does return the last item:

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.
except, when they find their return context is SCALAR they chose not to return a list, but instead they return a scalar

A feat not reserved for map and grep, but shared by every operator and function in Perl, including, without a single exception, all user defined functions. Anything that is in non-void scalar context will return exactly one value. And nothing will ever be returned in void context.

It mostly a matter of words, but I think it is wrong to say "map and grep each return lists", when like any other perl function they can chose whether to return a list, a scalar or nothing at all depending upon the context in which they are called.

You are mostly right. Except that a function or operator cannot chose whether to return a list, a scalar or nothing at all. It's the context that makes the choice - not the function.

Replies are listed 'Best First'.
Re^7: Scalar context of slice (myth)
by BrowserUk (Patriarch) on Oct 04, 2008 at 02:22 UTC
    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.

      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.
      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.