Here is a working sample script
Well, not quite - for it to work, keys %file_tests should probably be keys %HoH instead. This is why it's a good idea to always Use strict and warnings!
Anyway, I am not sure if you are asking a question, or just want to provide this information - if it's the latter, then this post is probably better suited to Meditations (don't repost it though, someone can move it for you).
While the code you showed works, note that sub new_hash returns a list of values (the key/value pairs of %hash), this causes this list of values to be copied, which isn't necessary. Inside of sub new_hash, you could create a new anonymous hash by saying my $hash = { keyname => ... }; return $hash;, and then $hash holds a reference to this anonymous hash, which you can treat just like a regular scalar, meaning you can say things like my $child_hash = new_hash(); $HoH{ $child_hash->{keyname} } = $child_hash;, and all that gets copied over is the reference to the anonymous hash, while the data inside the anonymous hash doesn't get copied. (An alternative is sub new_hash { my %hash = ( ... ); return \%hash }.)
As for your two suggestions, note that $HoH{$key} = {%child_hash}; causes the key/value pairs in the hash to be copied one more time, which could be considered a bit wasteful. So if you have a hash %child_hash, personally I'd recommend the $HoH{$key} = \%child_hash; version, since then what you are storing is a reference to %child_hash, which doesn't require the contents of the hash to be copied. The only thing to keep in mind is that because we're talking about references, any changes to %child_hash afterwards will be reflected in $HoH{$key} and vice versa!
(Also, whenever I'm talking about copies above, note these are shallow copies, which means that if any of the values in the hash we are talking about are references, the copies of the hash will refer to the same data sub-structures.)
See also perlreftut, perlref and perldsc.
Also, in regards to your post here, see How do I change/delete my post?
In reply to Re: Add child hash to hash of hashes.
by haukex
in thread Add child hash to hash of hashes.
by bdalzell
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |