# snip... sub _lock_meta { my $self = shift; my $mode = shift || 'r'; my $meta_name = do { unless (exists $self->{metaname}) { $self->{metaname} = File::Spec->catfile($self->{path}, 'me +ta.info'); } $self->{metaname}; }; my $fh = IO::File->new($meta_name, $mode); if ($fh) { flock($fh, $mode eq 'r' ? LOCK_SH : LOCK_EX); } $fh; } sub _load_meta { my $self = shift; my $fh = $self->_lock_meta(); # only load it if it's been changed since the last load. my $s = stat($self->{metaname}); if ($s and $s->mtime() >= ($self->{metastamp} || 0) and $fh) { local $/; my $data = join '', $fh->getlines(); $self->{metastamp} = time(); $fh->close(); # release lock $self->{meta} = $self->_thaw($data); } } sub _save_meta { my $self = shift; my $fh = $self->_lock_meta('w'); $fh->print($self->_freeze($self->{meta})); $fh->close(); } sub _thaw { my $self = shift; my $data = shift; eval 'my ' . $data; } sub _freeze { my $self = shift; my $data = shift; require Data::Dumper; local $Data::Dumper::Indent = 0; join '', Data::Dumper::Dumper($data); } # snip...
In reply to Re: Need Example of Saving and Retrieving Hash from File
by Tanktalus
in thread Need Example of Saving and Retrieving Hash from File
by EchoAngel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |