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

Then you have a broken Perl.

$ perl -e'use warnings; my %keys={A=>"b"};' Reference found where even-sized list expected at -e line 1. $ perl -v This is perl, v5.10.0 built for i686-linux Copyr...

I still am fuzzy about the difference between “square brackets,” “braces,” and “parentheses.”

Square brackets create an array and return a reference to the array.
Curly brackets create a hash and return a reference to the hash.
Parens are like in Math. They change the order of operations.

From that, it follows that one never uses [] or {} when initialising an array or hash*, and that parenthesis aren't related to array or hash initialisation.

* — They might be used in initialising the value of an array or hash element's value, though.

Update: I added the last paragraph and the associated "*". I thought it was obvious, especially in the context of the other replies, but it might need saying.

Replies are listed 'Best First'.
Re^3: Why? (each...)
by Argel (Prior) on May 12, 2011 at 19:01 UTC
    In the context of this discussion, wouldn't it be that parenthesis indicate a list?

    Elda Taluta; Sarks Sark; Ark Arks

      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