in reply to Re: Scalar Vs. List context
in thread Scalar Vs. List context

The most commonly used functions expecting list context include push, pop,

pop does not impose list context.   pop only accepts one argument, an array, and that is in scalar context.

Update: on the other hand, push does impose a list context on everything after the initial array argument.

Replies are listed 'Best First'.
Re^3: Scalar Vs. List context
by ikegami (Patriarch) on Jul 12, 2009 at 07:01 UTC

    pop only accepts one argument, an array, and that is in scalar context.

    Depending on how you look at it, it's either evaluated in list context (technical reality) or neither since it's not evaluated as an array would in list and scalar context (practical reality).

    In no way is it evaluated in scalar context like you claim.

      I'm not sure it even gets to the point of evaluation :-). At least in Perl 5.8.8 attempts to pass a first parameter without an @ sigal produces compilation errors. You don't even need strictures to produce them:
      our @x; sub foo { print "wantarray? ", wantarray?1:0, "\n"; push @x, (1,2,3); return @x; } pop foo();

      generates

      Type of arg 1 to pop must be array (not subroutine entry) at Monks/Sni +ppet.pm line 7, near ");" Execution of Monks/Snippet.pm aborted due to compilation errors.

      The same compilation error results if one tries to use a subroutine to generate the first parameter of push as well. Replacing pop foo() with push foo(), 4 results in

      Type of arg 1 to push must be array (not subroutine entry) at Monks/Sn +ippet.pm line 7, near "4;" Execution of Monks/Snippet.pm aborted due to compilation errors.

      Best, beth

        I'm not sure it even gets to the point of evaluation :-).

        Are you saying it's a no-op?

        push @{[ print "It definitely gets evaluated" ]}, 1;

        To revisit what I already said, it's useless to talk about the context in which the @a from push @a, ... is evaluated because it doesn't return what it normally returns in either list or scalar context.

      $ perl -le'print prototype "CORE::pop"' ;\@

      A reference is a scalar and pop's first argument is a scalar reference to an array.

        exactly, but that's different from "evaluating in scalar context".

        like in $foo=\@bar, $foo is not the length of @bar!

        IIRC in Perl 6 there is less confusion, since the scalar of an array is the array-reference.

        Cheers Rolf

        I never said it didn't result in a scalar. I said it wasn't evaluated in scalar context.
        $ perl -MO=Concise -e'pop @a' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <1> pop vK/1 ->6 4 <1> rv2av[t2] lKRM/1 ->5 <-- "l"ist context. 3 <#> gv[*a] s ->4 -e syntax OK

        I also said it's silly to consider the context of something subjected to the \@ prototype.