This is so small compared to the rest of the posts here in CUFP, I hesitated for a while before deciding to submit anyway:
package cache { # no particular reason to use them, # but the syntax sugar is *so* sweet use experimental 'signatures'; use base 'Storable'; my %paths; sub new($class,$path) { my $self = eval { return Storable::retrieve($path) } || bless {}, $class; $paths{$self} = $path; return $self; } sub DESTROY($self) { $self->store($paths{$self}); } }
How to use: The class uses inside-out objects so you could use objects as ordinary hashes with no reserved fields. This will get slower the bigger your cache gets because there is no RLU eviction, everything is stored in memory and the whole store has to be loaded from disk on startup and serialised on shutdown. Still, for small scripts I find it useful.

Replies are listed 'Best First'.
Re: A hash that stores itself between runs
by Jenda (Abbot) on Sep 27, 2018 at 12:17 UTC

      Right, BerkeleyDB and friends. Thank you for reminding me.

      I could provide excuses like "but that one only stores strings" or "but I don't want to pull outside dependencies" or "but it's only a dozen lines or so"... there are many solutions like it, but this one is mine. Don't worry, I'm not a professional programmer.