xref: /openbsd-src/gnu/usr.bin/perl/dist/Storable/t/testlib.pl (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!perl -w
2use strict;
3use vars '$file';
4
5$file = "storable-testfile.$$";
6die "Temporary file '$file' already exists" if -e $file;
7
8END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
9
10use Storable qw (store retrieve freeze thaw nstore nfreeze);
11
12sub slurp {
13  my $file = shift;
14  local (*FH, $/);
15  open FH, "<$file" or die "Can't open '$file': $!";
16  binmode FH;
17  my $contents = <FH>;
18  die "Can't read $file: $!" unless defined $contents;
19  return $contents;
20}
21
22sub store_and_retrieve {
23  my $data = shift;
24  unlink $file or die "Can't unlink '$file': $!";
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
38$file;
39