Assigning anonymous hash to a list

Abigail abigail at abigail.be
Wed Jul 31 00:25:07 BST 2013


On Tue, Jul 30, 2013 at 06:52:32PM -0400, Joseph Werner wrote:
> Hmmm.  Yitzchak Scott-Thoennes, I recognize, Abigail I do not (perhaps
> I should?)
> 
> So, Yitzchak, is this, in fact, a simple matter of operator precedence
> in that the ',' operator is of lower precedence than the assignment
> operator (absurd though that may seem)  AND not a case of a scalar
> value (which is the result of a Perl expression involving a ','
> operator) being offered to a  list assignment?
> 


Because the comma is in scalar context (it's in void context, but that's
a special case of scalar context), it will apply scalar context when
it evaluates its arguments. 

Its leftmost argument is an assigment. Hence, the assignment will be
evaluated in scalar context. On the LHS of the assignment is a list,
which makes the assignment is list assignment (in scalar context). Because
it's a list assignment, the RHS of the assignment is evaluated in list
context.



The difference between

    my ($str, $ref) = "text", { };

and

    my ($str, $ref) = ("text", { });

is precedence. In the former expression, the comma (the one following "text")
is the top level operator. In the latter expression, the (list)assignment
is the top level operator. Both expressions have a list assignment, but
only the first expression has a comma in scalar (void) context.



Abigail


More information about the london.pm mailing list