in reply to Re^2: Why? (each...)
in thread Why? (each...)

In the context of this discussion, wouldn't it be that parenthesis indicate a list?

Elda Taluta; Sarks Sark; Ark Arks

Replies are listed 'Best First'.
Re^4: Why? (each...)
by ikegami (Patriarch) on May 12, 2011 at 19:47 UTC

    In the current context, they definitely do NOT create a list. They override the relative precedence of the comma and assignment operators.

    my %keys = (A => "b"); - --- 1 2 List has two items
    my %keys = A => "b"; ------------ --- 1 2 List still there without parens

    Parens never create a list as far as I'm concerned, but people have debated me on two specific cases:

    • In some places where a list is already being created, «()» is needed to indicate the list contains no elements. The parens don't create the list (since it would also get created if one used «@a» instead of «()»), they simply indicate the list is empty.

    • In some places, the presence of parens affect the choice of operator to one that creates a list. (e.g. «$x = f();» vs «($x) = f();»). It's actually the assignment operator (not the parens) that creates the list as is gets created for «@a = f();» as well. Although both indirectly create a list, it makes no sense to say that parens create a list since noone says "«@a» creates a list".

    Parens, curlies and square brackets have other uses than those I described, of course, but I doubt the other uses create confusion.

    Lists value are created by operators that always take a list (e.g. foreach, list assignment, function calls, etc), and list literals are created by the comma operator.

    foreach ($x) { } @a = @b; f $x; ^^ ^^ ^^ ^^ list list list list value value value value $i, $j; @a = ($i, $j); ^^^^^^ ^^ ^^^^^^^^ list list list value literal value (from list literal)
      I was thinking of two things: when "()" is used to indicate you want or have an empty list, and then more for reading source code, where parens often indicate to the reader that he is looking at a list. The parens in foreach(), my($x,$y,$z)=@_, etc. Or e.g. in the case of %hash=(x=>"a",y="b") the parens indicate a list of key/value pairs. Or to put it differently, they indicate it's not an array and not a hash (and not a scalar).

      Elda Taluta; Sarks Sark; Ark Arks

        where parens often indicate to the reader that he is looking at a list.

        You say foreach (), I say while () and if ().

        You say my ($x,$y,$z) = @_;, I say ($x+$y+$z)*$n.

        The parens in foreach(),

        The parens of foreach not even part of the expression, and the list is still created when you omit them.

        say foreach "a", "b";

        Or to put it differently, they indicate it's not an array and not a hash (and not a scalar).

        Really?

        $s = ($x); %h = ($x); %h = ([]);

        Your model has too many holes to count :(

        Simple model: A list is created whenever something is evaluated in list context.