store#

Serialise a referenced structure and write it to a named file, overwriting any previous contents.

Synopsis#

use Storable qw(store retrieve);
store(\%data, "cache.bin") or die "write failed";
my $data = retrieve("cache.bin");

What you get back#

Returns 1 on success. The file at $path is created (or truncated if it already exists) and written with the same native-byte-order payload that freeze would produce.

Examples#

Persist a cache and reload it on startup:

store({ built_at => time(), entries => \@entries }, "cache.bin");

## ... later, possibly a different process:

my $cache = retrieve("cache.bin");

Atomic-ish replace via rename:

store(\%state, "state.bin.tmp");
rename "state.bin.tmp", "state.bin";

Edge cases#

  • First argument must be a reference; croaks with Not a reference otherwise.

  • If the file cannot be opened for writing, croaks with Storable: can't open file for writing.

  • Files written by store are only portable between machines of the same endianness and integer size. Use nstore for cross-architecture transport.

Differences from upstream#

  • Payload format differs from upstream Storable’s; files are not interchangeable with perl5’s store output. Pinned by t/81-xs-native/Storable/030-store-retrieve.t.

See also#

  • retrieve — matching reader for files written by store.

  • nstore — portable variant (writes network byte order).

  • freeze — return bytes instead of writing a file.

  • store_fd — write to an already-open filehandle.