in reply to Construction/initialisation of empty hash

The {} operator creates a new anonymous hash reference, so when you assign {} to a hash you're really creating a key of the stringified anonymous hash reference. If you run your code with warnings or -w you'll see a helpful warning e.g
$ perl5.00504 -MData::Dumper -we '%h={};$h{a}=1;print Dumper \%h' Reference found where even-sized list expected at -e line 1. $VAR1 = { 'a' => 1, 'HASH(0x8922a64)' => undef };
See. perlref and perldata for more info on the {} operator. As for the changes of output between perl versions, I can't seem to replicate them as the above example illustrates.
HTH

_________
broquaint

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

    Thanks, that's a helpful one-liner.

    Perl 5.8.0 gives the output you show, however, my older version (which is 5.004_04) gives:

    $ /opt/perl5/bin/perl -MData::Dumper -we '%h={};$h{a}=1;print Dumper \ +%h' Odd number of elements in hash list at -e line 1. $VAR1 = { 'a' => 1 };
      Just speculating; did perl used to drop off odd elements in hash initialization instead of adding an implied undef to make things even out?