in reply to Construction/initialisation of empty hash

Checking, I find that perl5.004 appears to throw away the last key whenever a hash is initialised from a list with an odd number of elements, whereas perl5.004_05 and later act as if there were an extra undef at the end of the list to make it even.

As a result, %a = (1, 2, 3) leaves %a containing one key ('1') in perl5.004 and two keys ('1', '3') in newer perls. I guess this came in as a bug fix sometime after the initial perl5.004 release, but I don't know exactly when, and I don't have any of the intermediate versions to check.

Even perl5.004 warns "Odd number of elements in hash list", so this is just another reason always to turn on warnings. :)

Hugo

Replies are listed 'Best First'.
Re: Re: Construction/initialisation of empty hash
by muntfish (Chaplain) on Apr 27, 2004 at 18:38 UTC

    Thanks hv. The change must have been between 5.004_04 and 5.004_05 since with my 5.004_04 I get the old style behaviour.

    Totally agree about warnings - but this code wasn't written by me! It has been in production for several years (with warnings turned off!) and this issue has only come to light just now with an upgrade to 5.8.0.

    Anyhow since the solution is to change from {} to () and it only happens in 2 or 3 places, I'm not too worried about it.