in reply to how to limit size of hash

albert,
You appear to be in a catch-22. For some reason, the normal solution ($dbh->prepare_cached) is not working and the alternatives will likely defeat the purpose. There are performance impacts using tied interfaces on top of managing your own cache.

Other than the modules already suggested, Tie::Cache looks like a close match to what you want. It works on the least recently used concept allowing you to specify limits based on quantity and size. I also rolled my own because it looked like fun:

#!/usr/bin/perl package Fixed_Hash; require Tie::Hash; @ISA = (Tie::StdHash); my ($used, $max, %cache); sub TIEHASH { my $class = shift; my $limit = shift || 100; $max = int ( $limit / 2 ); bless {} , $class; } sub FETCH { my ($self, $key) = @_; return exists $cache{$key} ? $cache{$key} : $self->{$key}; } sub STORE { my ($self, $key, $val) = @_; return if exists $cache{$key} && $cache{$key} eq $val; return if exists $self->{$key} && $self->{$key} eq $val; ++$used; if ( $used >= $max ) { %cache = %{ $self }; %{ $self } = ($key, $val); $used = 1; } else { $self->{$key} = $val; } return; } package main; use strict; use warnings; tie my %fixed_hash, 'Fixed_Hash', 20;
This naive approach divides the user defined max between two hashes. When the limit has been reached, the first hash is moved to the second and the first starts over.

Cheers - L~R