in reply to tieing HoH

When you execute the statement $hash{toby}{ip} = '111'; on a newly created (and tied) hash, you are asking perl to

  1. Create a key named 'toby' in the hash (named '%hash').
  2. Set the value associated with that key to a new, empty, untied anonymous hash.
  3. Create a new key named 'id' in that anonymous hash.
  4. Set the value associated with that key ('id') to the string '111'.

The actual sequence perl performs thise steps is probably different, but they must all be performed.

In other words, the assignment $hash{toby}{id} = '111'; isn't an assignment to the hash %hash;

It is an assignment to the key 'id', within the anonymous hash that is the value of the key 'toby' within the hash %hash.

As %hash is newly created and the key 'toby' does not exist, perl has to 'autovivify' (ie. create) the key 'toby' and assign an anonymous hash to it, before it can create the 'id' key and assign '111' to it.

Not sure if that clarifies anything?

The short answer to "Could this be problem 'cause it is not one-level-hash..", is Yes!.

If you want to tie a multilevel hash, you would have to inspect the values STOREd into your base-level hash, and if they are hash references, tie these also. This is non-trivial to get right.

Hope that help is some way:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: tieing HoH
by bugsbunny (Scribe) on Jan 14, 2004 at 15:14 UTC
    yep i get the picture now...
    only thing I forgot to mention is that keys "toby" and "ip" exists already..
    They are populated inside Tie::HashDF with other methods by default..I have cleared them from this example to be easier for reading
    Havent tested keys that doesnt exist, will check that..