package ArrayStore; use strict; use warnings; use IO::File; use Fcntl qw(:flock); use Storable qw(freeze thaw); sub new{ my $class = shift; my %cfg = ( file => '', flock => 0, @_); return eval{ my $fh = IO::File->new; $fh->open($cfg{file}, O_CREAT|O_BINARY|O_RDWR) or die $!; my $isflock = $cfg{flock} ? flock $fh, LOCK_EX : -1; warn "flock is not supported" unless $isflock; bless{ FH => $fh }, $class; } } # Array aus Datei lesen sub read{ my $self = shift; $self->{FH}->seek(0,0); read($self->{FH}, my $bin, -s $self->{FH}); return length $bin ? thaw($bin) : []; } # Array nach Datei serialisieren sub write{ my $self = shift; my $r = shift; $self->{FH}->seek(0,0); $self->{FH}->truncate(0); $self->{FH}->print(freeze($r)); } 1;########################################################################### __END__ use Data::Dumper; my $as = ArrayStore->new( file => 'stbin', flock => 1 ) or die $@; $as->write([{},{}]); my $r = $as->read; print Dumper $r;