DirDB-Storable

 view release on metacpan or  search on metacpan

Storable.pm  view on Meta::CPAN

		return {};
	  };
	};

	if(defined wantarray){
		local $/ = undef;
		open FSDBFH, "<$rootpath$key";
		$value = <FSDBFH>;
	};
	unlink "$rootpath$key";
	$value;
};

sub CLEAR{
	my $ref = shift;
	my $path = $$ref;
	opendir FSDBFH, $path or croak "opendir $path: $!";
	my @ents = (readdir FSDBFH );
	while(defined(my $entity = shift @ents )){
		$entity =~ /^\.\.?\Z/ and next;
		$entity = join('',$path,$entity);
		if(-d $entity){
		   eval {recursive_delete $entity};
		   $@ and  croak "could not delete (sub-container?) directory $entity: $@";
		};
		unlink $entity;
	};
};

{

   my %IteratorListings;

   sub FIRSTKEY {
	my $ref = shift;
	my $path = $$ref;
	opendir FSDBFH, $path or croak "opendir $path: $!";
	$IteratorListings{$ref} = [ grep {!($_ =~ /^\.\.?\Z/)} readdir FSDBFH ];

	#print "Keys in path <$path> will be shifted from <@{$IteratorListings{$ref}}>\n";
	
	$ref->NEXTKEY;
   };

   sub NEXTKEY{
	my $ref = shift;
	#print "next key in path <$$ref> will be shifted from <@{$IteratorListings{$ref}}>\n";
	@{$IteratorListings{$ref}} or return undef;
	my $key = shift @{$IteratorListings{$ref}};
	if ($key =~ s/^ //){
		if ($key = m/^ /){
			# we have unescaped a leading space.
		}elsif ($key eq 'EMPTY'){
			$key = ''
		#}elsif($key eq 'REF'){
		# 	return $ref->NEXTKEY();	# next
		#}elsif($key =~ m/^ARRAY){
		# 	return $ref->NEXTKEY();	# next
		}else{
			# per-container metadata does not
			# appear in iterations through data.
			return $ref->NEXTKEY();	# next
		}
	};
	wantarray or return $key;
	return @{[$key, $ref->FETCH($key)]};
   };
   
   sub DESTROY{
       delete $IteratorListings{$_[0]};
   };
 
};




1;
__END__

=head1 NAME

DirDB::Storable - DirDB extended to pass complex things to Storable

=head1 SYNOPSIS

  use DirDB::Storable;
  tie my %PersistentData, 'DirDB', "./data";

=head1 DESCRIPTION

DirDB::Storable is an extended version of DirDB data
persistence tool that uses the Storable module to store and retreive
data that is other than scalars and hash references.

 tie my %d => DirDB, '/tmp/foodb';
 
 $d{ref1}->{ref2}->{ref3}->{ref4} = 'something'; 
 # 'something' is now stored in /tmp/foodb/ref1/ref2/ref3/ref4
 
 $d{ref5} = [1..99]; 
 # an array reference, with data, is now stored in /tmp/foodb/ref5/\ Storable
 
=head2 RISKS

Besides the potential stale lock problem with DirDB, there is a risk
of confusion about what is and isn't tied into the persistence framework.

	$d{ref5}->[33] = 'cheese'; # not written out
	
	{ 
	  my $tmp = $d{ref5};
	  $tmp->[33] = 'cheese';
	  $d{ref5} = $tmp;        # a work-around
	}

=head2 TO DO

a set of Tie::Storable modules that the retreived stored objects could be tied
to instead of the above-demonstrated hack would be cool.



( run in 0.623 second using v1.01-cache-2.11-cpan-5511b514fd6 )