gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

Playing with HASH references, I found a strange bug! Take look in the result of this code (tested with Perl-5.6.1 and Perl-5.8.0 on Win32):
my $hash = {} ; $hash->{key1} = 'k1' ; $hash->{key1}{key2} = 'k2' ; print ">> $hash->{key1}\n" ; print ">> $hash->{key1}{key2}\n" ; __OUTPUT__ >> k1 >> k2
Note that the k2 ($hash->{key1}{key2}) is printed! But k1 ($hash->{key1}) already exist, and isn't a HASH ref!

What should happens is to exist only one option, k1 or k2! Not the both! I found the error using Storage, to save the HASH tree in a file. But Storage only save k1, like should be. In other words, k2 only exist in the memory, not in the HASH tree.

If you try to test the ref() of $hash->{key1} you also get undef.

Soo, is this documented? Is right or wrong? How to know if k1 is also a HASH ref?

Please, test the code and see if this output works in other OS too (tested on Win32 only).

Note that this only work for HASH reference, if you use %hash, work normal.

Graciliano M. P.
"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Strange HASH reference BUG! Dual value for a key with sub-key!
by grep (Monsignor) on May 21, 2003 at 06:40 UTC
    Not a bug.

    If you use strict or put this at the end of your code you'll get your answer.

    print ">> $k1{key2}\n" ;
    It is a soft reference.

    As a general rule please run your code with 'use strict' and 'use warnings' before you post it here as a question.

    grep
    Mynd you, mønk bites Kan be pretti nasti...

Re: Strange HASH reference BUG! Dual value for a key with sub-key!
by BrowserUk (Patriarch) on May 21, 2003 at 06:43 UTC

    What you've done is used a symbolic reference to create a second hash %k1.

    use Data::Dumper; my $hash = {} ; $hash->{key1} = 'k1' ; $hash->{key1}{key2} = 'k2' ; print ">> $hash->{key1}\n" ; print ">> $hash->{key1}{key2}\n" ; print Dumper $hash; print Dumper \%k1; ^Z >> k1 >> k2 $VAR1 = { 'key1' => 'k1' }; $VAR1 = { 'key2' => 'k2' };

    As you don't have strict turned on, the value of $hash{key1} was used as the hash name in the second assignment and it was autovivified for you. And again when interpolated in the print statement.

    You told the compiler you knew what you were doing (by not useing strict), so it went right ahead and did what you asked:) He he.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Strange HASH reference BUG! Dual value for a key with sub-key!
by djantzen (Priest) on May 21, 2003 at 06:45 UTC

    You're inadvertently creating a symbolic reference by using the string 'k1' as a hash. Using the 'V' option under the perl debugger you'll see something like the following (Note: I removed the my declaration so the original hash would show up in the symbol table) :

    %hash = ( 'key1' => 'k1' ) ...... ...... %k1 = ( 'key2' => 'k2' )

    Just one more reason to use strict :) Note as well that it doesn't matter if it's a hash or hashref.


    "The dead do not recognize context" -- Kai, Lexx
Re: Strange HASH reference BUG! Dual value for a key with sub-key!
by mce (Curate) on May 21, 2003 at 06:58 UTC
    Hi,

    I made a node about this a while ago. Have a look at 192028.

    ...And is it not a bug.....
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium