Storing Data Structures to Disk

Problem

You want to save your large, complex data structure to disk so you don’t have to build it up each time your program runs.

Solution

Use the CPAN module Storable’s store and retrieve functions:

use Storable; 
store(\%hash, "filename");

# later on...  
$href = retrieve("filename");        # by ref
%hash = %{ retrieve("filename") };   # direct to hash

Discussion

The Storable module uses C functions and a binary format to walk Perl’s internal data structures and lay out its data. It’s more efficient than a pure Perl and string-based approach, but it’s also more fragile.

The store and retrieve functions expect binary data using the machine’s own byte-ordering. This means files created with these functions cannot be shared across different architectures. nstore does the same job store does, but keeps data in canonical (network) byte order, at a slight speed cost:

use Storable qw(nstore); 
nstore(\%hash, "filename"); 
# later ...  
$href = retrieve("filename");

No matter whether store or nstore was used, you need to call the same retrieve routine to restore the objects in memory. The producer must commit to portability, but the consumer doesn’t have to. Code needs only to be changed in one place when the producer changes their mind and the code thus offers a consistent interface on the consumer side, who does not need to know or care.

The store and nstore functions don’t lock any of the files they work on. If you’re worried about concurrent access, open the file yourself, ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.