Nicholas Clark 提到:
So if the semicolon is replaced with a comma, like this,

    my @x := [{1+1}, {2+2}];

the {} acts as a hash constructor, and @x is [{2 => undef}, {4 => undef}] ?

No, {} acts as a closure constructor, and @x contains two closures that returns 2 and 4 respectively when called:

    @x[0](); # 2
    @x[1](); # 4

Basically, {} is only a hash constructor if the inside is either empty, or contains only a list starting with a pair or a hash:

    $hash = { };
    $hash = { %stuff };
    $hash = { "a" => 1 };
    $hash = { "a" => 1, $b, $c, %stuff, @nonsense };

    $code = { ; };
    $code = { @stuff };
    $code = { "a", 1 };
    $code = { "a" => 1, $b, $c ==> print };

The examples above are from L<S04/"Statement parsing">.

Cheers,
Audrey

Reply via email to