Here's a little demo:
use strict;
use warnings;
use Data::Dumper;
my @aoh = ( { a => 1 } );
my $file = 'diskstats.perldata';
out( $file, \@aoh );
undef @aoh;
@aoh = in( $file );
sub out {
my ( $file, $aoh_ref ) = @_;
open my $fh, '>', $file
or die "Can't write '$file': $!";
local $Data::Dumper::Terse = 1; # no '$VAR1 = '
local $Data::Dumper::Useqq = 1; # double quoted strings
print $fh Dumper $aoh_ref;
close $fh or die "Can't close '$file': $!";
}
sub in {
my ( $file ) = @_;
open my $fh, '<', $file
or die "Can't read '$file': $!";
local $/ = undef; # read whole file
my $dumped = <$fh>;
close $fh or die "Can't close '$file': $!";
return @{ eval $dumped };
}
Notes:
- I don't check here whether the eval succeeded, but that would be a good idea.
- These are bad sub names.
- These are not general dump/save subs. They're meant just for arrays.
- Consider using YAML or Storable instead since it doesn't have the possibility of executing things if someone hostile gets a hold of the dump file.