develooper Front page | perl.perl5.porters | Postings from April 2018

Re: [perl #133134] map{ /.../; { %+ } } @data wrongly flattenshashes into arrays

Thread Previous | Thread Next
From:
Sawyer X
Date:
April 21, 2018 08:33
Subject:
Re: [perl #133134] map{ /.../; { %+ } } @data wrongly flattenshashes into arrays
Message ID:
48da50b3-cf49-feb1-8e4a-807e3772e033@gmail.com


On 04/21/2018 10:28 AM, Dave Mitchell wrote:
> On Fri, Apr 20, 2018 at 01:25:49PM -0700, via RT wrote:
>> # New Ticket Created by   
>> # Please include the string:  [perl #133134]
>> # in the subject line of all future correspondence about this issue. 
>> # <URL: https://rt.perl.org/Ticket/Display.html?id=133134 >
>>
>>
>>
>> This is a bug report for perl from jim.avera@gmail.com,
>> generated with the help of perlbug 1.40 running under perl 5.26.1.
>>
>> -----------------------------------------------------------------
>> If used inside map{...}, the construct
>>
>>   { %+ }
>>
>> results in a flat list of keys and values, not a ref to a hash.  
> An '{' at the start of a statement is ambiguous: it could be the start
> of a new block, or the start of a hash constructor. In this situation, perl
> makes an educated guess based on the first few characters following the
> '{'. In this case, its guess didn't match your hopes.
>
> You can avoid the ambiguity by providing perl with more information; for
> example:
>
>     %a = qw(a 1 b 2);
>
>     sub f1 { 1; { %a } }         # ambiguous
>     sub f2 { 1; scalar { %a } }  # unambiguous: a hash ref constructor
>     sub f3 { 1; {1;  %a } }      # unambiguous: a block

Also:

    sub f3 { 1; {; %a } }

I picked up the following pattern from rjbs:

    map +{ 'key' => 'value' }, @values; # hashref expression
    map +( 'key' => 'value' ), @values; # hash expression
    map {; ... } @values; # code block

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About