perl / Git / SVN / Memoize / YAML.pmon commit Merge branch 'jn/make-assembly-in-right-directory' into maint (dbf64e1)
   1package Git::SVN::Memoize::YAML;
   2use warnings;
   3use strict;
   4use YAML::Any ();
   5
   6# based on Memoize::Storable.
   7
   8sub TIEHASH {
   9        my $package = shift;
  10        my $filename = shift;
  11        my $truehash = (-e $filename) ? YAML::Any::LoadFile($filename) : {};
  12        my $self = {FILENAME => $filename, H => $truehash};
  13        bless $self => $package;
  14}
  15
  16sub STORE {
  17        my $self = shift;
  18        $self->{H}{$_[0]} = $_[1];
  19}
  20
  21sub FETCH {
  22        my $self = shift;
  23        $self->{H}{$_[0]};
  24}
  25
  26sub EXISTS {
  27        my $self = shift;
  28        exists $self->{H}{$_[0]};
  29}
  30
  31sub DESTROY {
  32        my $self = shift;
  33        YAML::Any::DumpFile($self->{FILENAME}, $self->{H});
  34}
  35
  36sub SCALAR {
  37        my $self = shift;
  38        scalar(%{$self->{H}});
  39}
  40
  41sub FIRSTKEY {
  42        'Fake hash from Git::SVN::Memoize::YAML';
  43}
  44
  45sub NEXTKEY {
  46        undef;
  47}
  48
  491;
  50__END__
  51
  52=head1 NAME
  53
  54Git::SVN::Memoize::YAML - store Memoized data in YAML format
  55
  56=head1 SYNOPSIS
  57
  58    use Memoize;
  59    use Git::SVN::Memoize::YAML;
  60
  61    tie my %cache => 'Git::SVN::Memoize::YAML', $filename;
  62    memoize('slow_function', SCALAR_CACHE => [HASH => \%cache]);
  63    slow_function(arguments);
  64
  65=head1 DESCRIPTION
  66
  67This module provides a class that can be used to tie a hash to a
  68YAML file.  The file is read when the hash is initialized and
  69rewritten when the hash is destroyed.
  70
  71The intent is to allow L<Memoize> to back its cache with a file in
  72YAML format, just like L<Memoize::Storable> allows L<Memoize> to
  73back its cache with a file in Storable format.  Unlike the Storable
  74format, the YAML format is platform-independent and fairly stable.
  75
  76Carps on error.
  77
  78=head1 DIAGNOSTICS
  79
  80See L<YAML::Any>.
  81
  82=head1 DEPENDENCIES
  83
  84L<YAML::Any> from CPAN.
  85
  86=head1 INCOMPATIBILITIES
  87
  88None reported.
  89
  90=head1 BUGS
  91
  92The entire cache is read into a Perl hash when loading the file,
  93so this is not very scalable.