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

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

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
April 21, 2018 08:28
Subject:
Re: [perl #133134] map{ /.../; { %+ } } @data wrongly flattenshashes into arrays
Message ID:
20180421082842.GA2839@iabyn.com
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

    print ((f1())[0], "\n");
    print ((f2())[0], "\n");
    print ((f3())[0], "\n");

which outputs:

    a
    HASH(0x22314e8)
    a


-- 
"I used to be with it, but then they changed what ‘it’ was, and now what
I’m with isn’t it. And what’s ‘it’ seems weird and scary to me."
  -- Grandpa Simpson
(It will happen to you too.)

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