http://www.perlmonks.org?node_id=719276


in reply to Re^2: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

Are there cases where something that's unambiguously a list (and not an array) returns anything else than the last item?

A regular expression match with the /g flag, in scalar context.

A map or grep expression in scalar context.

The empty list my $x = () = some_expression; construct, in scalar context.

split, chomp, ....

A bare hash. (Want to argue over what a list is? Is it multiple, comma-separated expressions or something that pushes one or more items onto the internal stack? I'm not sure lists even exist as a language-level construct in Perl 5.)

Replies are listed 'Best First'.
Re^4: If you believe in Lists in Scalar Context, Clap your Hands
by moritz (Cardinal) on Oct 24, 2008 at 15:28 UTC
    One could argue that built-ins like map and grep don't return a list in scalar context, but are context sensitives themselves, so they are not fit for explaining "a list in scalar context". Likewise a regex match with /g.

      Sure, but you can also argue that 1, 2, 3, isn't a list of values but a list of expressions. Is this a list:

      my $x = ($y = 1), ($z = 2);

      How about this?

      s{\\}{\\\\}g, print

      Or this?

      foo => scalar param( 'foo' )

      It's difficult to tell without seeing the context... which makes me wonder if lists do exist, or if we should talk about the value of expressions instead.

Re^4: If you believe in Lists in Scalar Context, Clap your Hands
by jwkrahn (Abbot) on Oct 25, 2008 at 02:15 UTC

    chomp???   How did chomp get in there?   It never returns a list.

      my @x = chomp <DATA>;

      Bingo, @x is gets a one-element list -- or at least, it's a list in list context and a scalar in scalar context. Tricky! Here's a mind-bender. What's this?

      1, 2, 3

      It could be a three-element list, in list context, or it could be a scalar and two void expressions (which get optimized away), in scalar context. In void context, it could be nothing.

      Update: Reworded one sentence.

        So, then, scalar also returns a list in list context?   (BTW, @x is an array, not a one element list.)

      when used with a list, it returns the total number of characters chomped... and not a list (so, you are right that there is no "list context" to it.
      []s, HTH, Massa (κς,πμ,πλ)