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

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

Thread Previous | Thread Next
From:
demerphq
Date:
April 23, 2018 08:06
Subject:
Re: [perl #133134] map{ /.../; { %+ } } @data wrongly flattenshashes into arrays
Message ID:
CANgJU+VnMn8hcDvAHyV4hCyAiikREB8CLwG5YxHNFSwHMS4ZWw@mail.gmail.com
On 21 April 2018 at 10:33, Sawyer X <xsawyerx@gmail.com> wrote:
>
>
> 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:

Really? I call that the abigail pattern. :-)

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

Yep. Parens also has the same effect iirc.

cheers,
Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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