in reply to A list returns its last, an array returns its weight

I've said it before and I'll say it again. There is no such thing as a list in scalar context. If I ask you "what does this function return?" and show you sub foo { (1,10,100) }, you can't be sure. Maybe it returns a list of three numbers, or perhaps it returns the number 100. In scalar context, the comma operator evaluates its left operand, discards the result, and returns the right operand. No list involved.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: A list returns its last, an array returns its weight
by broquaint (Abbot) on Nov 13, 2002 at 11:12 UTC
    So lists in scalar context are really just reduced to a list of expressions evaluated with the comma operator e.g
    sub with_parens { ('foo', 'bar', 'baz') } # is equivalent to the following in scalar context sub without_parens { 'foo', 'bar', 'baz' } print scalar with_parens(), $/; print scalar without_parens(), $/; __output__ baz baz
    Or am I off the mark here?

    _________
    broquaint

      Well, it's that lists don't exist in scalar context.

      In scalar context, a comma-separated group of expressions is a bunch of comma operators doing their thing. In list context, a comma-separated group of expressions is a list.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        In list context, a comma-separated group of expressions is a list.
        ... as long as the CSGoE is surrounded by parens e.g
        my @foo = "one", "two", "three"; my @bar = ("one", "two", "three"); print @foo, $/, @bar, $/; __output__ one onetwothree
        Otherwise it's just a CSGoE :)
        HTH

        _________
        broquaint

        update: ignore me, I'm wrong
        broquaint goes off to study perlop

        I thought the contexts were SCALAR and ARRAY. I did not know where was a list context. Oh, hold it, just because the function is called wantarray one cannot assume that is checking for array context. Sigh.

        Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality