Now, this will work (or something like it, I don't remember the syntax):
%hash = {key1 => 'value1'}
$hash_of_hash->{hash1} = \%hash
The syntax makes a lot of sense when you read the specs. It does create lots of problems when you try to create complex data structures in practice. It's also complex enough (with enough corner cases) that I can't remember it if I don't use it every week - but maybe that's just me.
By the way, I don't remember whether I must read the above structure as:
$hash_of_hash->{hash1}{key1}
or
$($hash_of_hash->{hash1}){key1}
I also don't remember if it is equal to the one you defined. I know it's in the documentation, and it's very clear there. It's just some corner case that I never bothered to memorize for any long time.
The quick way to remember this is that hashes naturally consume and supply lists. When a hash consumes a list, it alternates to assigning list items as keys and values. When a hash is supplied on the right hand side of an assignment, it's keys and values are supplies as a list in alternating order. Values for a hash or array must be a regular variable (scalar), or a reference to some item (such as an object, hash, array or scalar).
It also helps to know that '=>' is really just syntactic sugar for a comma that also implicitly quotes the word to its left, and not a special operator for hashes. These 2 lines are the same (in Perl 5):
Yeah, I had quite a bit more in my reply originally, including that and code snippets with assignment back and forth between a hash and array, but decided I didn't need to turn this article into a Perl 5 tutorial ;)
%hash = {key1 => 'value1'}
$hash_of_hash->{hash1} = %hash
Now, this will work (or something like it, I don't remember the syntax):
%hash = {key1 => 'value1'}
$hash_of_hash->{hash1} = \%hash
The syntax makes a lot of sense when you read the specs. It does create lots of problems when you try to create complex data structures in practice. It's also complex enough (with enough corner cases) that I can't remember it if I don't use it every week - but maybe that's just me.
By the way, I don't remember whether I must read the above structure as:
$hash_of_hash->{hash1}{key1}
or
$($hash_of_hash->{hash1}){key1}
I also don't remember if it is equal to the one you defined. I know it's in the documentation, and it's very clear there. It's just some corner case that I never bothered to memorize for any long time.