in reply to Why? (each...)

Most curious.   Now... not being entirely a babe in the woods about such things, I did run this with use strict; use warnings; (on Perl 5.10.0; OS/X Snow Leopard), albeit not with use diagnostics;, and it didn’t say anything.

Truth be known, and after all these years, I still am fuzzy about the difference between “square brackets,” “braces,” and “parentheses.”   If you know what I mean.   (I presume that, if I asked for a show of hands right now, I would not be out here by myself...)

Oh, well.   “Now, you know.™”

Replies are listed 'Best First'.
Re^2: Why? (each...)
by ikegami (Patriarch) on May 11, 2011 at 20:37 UTC

    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.

      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)
Re^2: Why? (each...)
by Argel (Prior) on May 12, 2011 at 19:00 UTC
    Curly braces create an anonymous hash and return a reference to it.
    Square brackets create an anonymous array and return a reference to it.
    Parenthesis, in the context of this discussion, indicate a list.

    Anonymous in the sense that there is no named variable for the hash or array -- you have to access them via the reference.

    Elda Taluta; Sarks Sark; Ark Arks

      What does "indicates a list" mean?

      my %h = ($x); # List created for RHS. my %h = $x; # List created for RHS. my $s = ($x); # No list created for RHS. my $s = $x; # No list created for RHS.