1#!perl -w
2use strict;
3
4our $file = "storable-testfile.$$";
5die "Temporary file '$file' already exists" if -e $file;
6
7END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
8
9use Storable qw (store retrieve freeze thaw nstore nfreeze);
10
11sub slurp {
12  my $file = shift;
13  local (*FH, $/);
14  open FH, "<", $file or die "Can't open '$file': $!";
15  binmode FH;
16  my $contents = <FH>;
17  die "Can't read $file: $!" unless defined $contents;
18  return $contents;
19}
20
21sub store_and_retrieve {
22  my $data = shift;
23  unlink $file or die "Can't unlink '$file': $!";
24  local *FH;
25  open FH, ">", $file or die "Can't open '$file': $!";
26  binmode FH;
27  print FH $data or die "Can't print to '$file': $!";
28  close FH or die "Can't close '$file': $!";
29
30  return eval {retrieve $file};
31}
32
33sub freeze_and_thaw {
34  my $data = shift;
35  return eval {thaw $data};
36}
37
381;
39