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

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

Thread Previous | Thread Next
From:
ilmari
Date:
April 21, 2018 08:31
Subject:
Re: [perl #133134] map{ /.../; { %+ } } @data wrongly flattenshashes into arrays
Message ID:
d8ja7txezb3.fsf@dalvik.ping.uio.no
"James E Keenan via RT" <perlbug-followup@perl.org> writes:

> On Fri, 20 Apr 2018 20:25:49 GMT, jim.avera@gmail.com wrote:
>> 
>> 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.
>> For example,
>> 
>> map{ /^(?<a>A*)(?<b>B*)$/ or die; { %+ } } ("AB", "AABBB", "AAAB");
>> 
>> returns
>> 
>> ('a', 'A', 'b', 'B', 'a', 'AA', 'b', 'BBB', 'a', 'AAA', 'b', 'B')
>> 
>> instead of
>> 
>> ( {'a' => 'A', 'b' => 'B' }, etc. )
[…]
> Using perlbrew, I checked out various production releases of perl, ran
> your program in each and recorded the results.  See attachment.
>
> The program wouldn't compile in 5.8.9, but did in 5.10.1.

This is because the regex uses named captures, which were introduced in
5.10.

> From 5.10.1 the result was stable up through 5.16.3.  As of 5.18.4 the
> results differ each release.  (I haven't looked closely enough to see
> whether or not this is just reshuffling of hash elements.)

This is because %+ is a hash, and hash order is randomised since 5.18.

> So this does not appear to be a regression appearing for the first
> time in the 5.27 dev cycle.

It is not a bug at all. The reason is that the { %+ } gets interpreted
as a block statement, rather than a hashref constructor expression.  The
standard way to disambiguate it to an expression is to precede it with a
unary +:

$ perl -MDevel::Dwarn -e 'Dwarn([map{ /^(?<a>A*)(?<b>B*)$/ or die; +{ %+ } } ("AB")])'
[
  {
    a => "A",
    b => "B",
  },
]

This ticket should be closed.

- ilmari
-- 
"The surreality of the universe tends towards a maximum" -- Skud's Law
"Never formulate a law or axiom that you're not prepared to live with
 the consequences of."                              -- Skud's Meta-Law

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