http://www.perlmonks.org?node_id=474118

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

Update:Made a minor correction to the code example. (Thanks Frodo72)
I've been finding myself building a lot of hashes of hashes of hashes ... of arbitrary depth lately, and i dont think my scheme is the greatest. So im wondering if somebody has a better scheme or if im on the right track.

Here's a simplified version of what i am basically doing:

use strict; use warnings; my %big_hoh; while ( my $data = get_one_record(...) ) { my @key_list = get_arbitrary_length_key_list($data, ...); my $last_key = pop @key_list; my $storage_point = \%big_hoh; foreach my $key (@key_list) { if ( ! exists $storage_point->{$key} ) { $storage_point->{$key} = {}; $storage_point = $storage_point->{$key}; } } $storage_point->{$last_key} = $data; }
This works but it seems pretty nasty to me. If $data were not a hashref, im not sure that it would work properly. I thought about building up from the back end, but that seems like it will be even worse.

This is for building a data cache for a system where users can do various searches in a MySQL DB, and sometimes it makes sense to cache some of the data, to take some load off the DB server. This is the 3rd major revision of the engine, and this revision is mostly optimizations.