I'm looking for a solution to transparently persist Perl data structures (not even objects, but object support would be a plus) without circular references. I don't care that much about the backend, but I'd prefer JSON. The number of objects would be relatively low (a few thousands of hashrefs with about 5 keys each). By "transparent" persistence I mean that I don't want to have to commit changes to the storage backend every time I update the in-memory data structure.
Here's how the code would ideally look like:
my $ds;
...
# load the $ds data structure from 'myfile'
print $ds->{foo}->{bar}; # baz
$ds->{foo}->{bar} = 'quux';
... program dies, but the updated %hash has been persisted automatically in 'myfile'
# in another invocation
print $ds->{foo}->{bar}; # quux
So far I've looked at:
I've only found one promising module, DBM::Deep. The code is just like in the example, and you can load the data structure with
my $ds = DBM::Deep->new( "myfile.db" );
The format is binary, though. Not a big problem, since I can use JSON to export it in a human-readable format.
So, am I missing a module, and am I approaching the problem correctly at all?
To achieve your "transparency" goal, you're going to have to either abstract it into a framework (as chambwez suggested) or use tie
d variables which will save themselves to disk whenever they're updated. DBM hashes use tie
in this way, so DBM::Deep
is probably your best bet; everything else I'm aware of requires you to explicitly tell it when to write data out and/or caches writes in the name of performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With