Search code examples
perloperatorsoperator-precedenceassignment-operatorperl-hash

What is a difference while assigning hash in list context?


I have to expressions:

%MON =    months => 1, end_of_month => 'limit';      # months => undef
%MON =  ( months => 1, end_of_month => 'limit' );

Why first expression results only one key months with undef value? What is the difference between them?


Solution

  • See perlop. = has higher precedence than =>

    %MON =    months => 1, end_of_month => 'limit'; 
    

    Is equivalent to:

    (%MON = "months"), 1, "end_of_month", "limit"
    

    While:

    %MON =  ( months => 1, end_of_month => 'limit' );
    

    is:

    %MON = ("months", 1, "end_of_month", "limit")