Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

import Memoize 0.46 from CPAN

git-cpan-module:   Memoize
git-cpan-version:  0.46
git-cpan-authorid: MJD
git-cpan-file:     authors/id/M/MJ/MJD/Memoize-0.46.tar.gz
  • Loading branch information...
commit 84a67d2c0e665bcaba711891ab5106278fc7a095 1 parent a7fb95f
Mark Jason Dominus (陶敏修) authored September 07, 1998 schwern committed December 12, 2009
2  MANIFEST
@@ -4,6 +4,7 @@ MANIFEST
4 4
 Memoize.pm
5 5
 Makefile.PL
6 6
 Memoize/SDBM_File.pm
  7
+Memoize/Storable.pm
7 8
 t/correctness.t
8 9
 t/array.t
9 10
 t/speed.t
@@ -13,6 +14,7 @@ t/unmemoize.t
13 14
 t/tie.t
14 15
 t/tiefeatures.t
15 16
 t/tie_gdbm.t
  17
+t/tie_storable.t
16 18
 demo.pl
17 19
 demo2.pl
18 20
 TODO
2  Makefile.PL
... ...
@@ -1,7 +1,7 @@
1 1
 use ExtUtils::MakeMaker;
2 2
 WriteMakefile(
3 3
 	      NAME => 'Memoize',
4  
-	      VERSION => '0.45',
  4
+	      VERSION => '0.46',
5 5
 #	      'linkext' => {LINKTYPE => ''},
6 6
 	      'dist' => {COMPRESS => 'gzip', SUFFIX => 'gz'},
7 7
 );
73  Memoize.pm
@@ -8,10 +8,10 @@
8 8
 # same terms as Perl itself.  If in doubt, 
9 9
 # write to mjd-perl-memoize@plover.com for a license.
10 10
 #
11  
-# Version 0.45 beta $Revision: 1.8 $ $Date: 1998/09/05 03:43:53 $
  11
+# Version 0.46 beta $Revision: 1.9 $ $Date: 1998/09/07 19:42:52 $
12 12
 
13 13
 package Memoize;
14  
-$VERSION = '0.45';
  14
+$VERSION = '0.46';
15 15
 
16 16
 =head1 NAME
17 17
 
@@ -449,21 +449,48 @@ and shorter every time you call C<main>.
449 449
 
450 450
 =back
451 451
 
452  
-=head1 MY BUGS
  452
+=head1 PERSISTENT CACHE SUPPORT
453 453
 
454  
-Needs a better test suite, especially for the tied stuff.
455  
-That is why the version number is 0.45 instead of 0.50.
  454
+You can tie the cache tables to any sort of tied hash that you want
  455
+to, as long as it supports C<TIEHASH>, C<FETCH>, C<STORE>, and
  456
+C<EXISTS>.  For example,
  457
+
  458
+	memoize 'function', SCALAR_CACHE => 
  459
+                            [TIE, GDBM_File, $filename, O_RDWR|O_CREAT, 0666];
  460
+
  461
+works just fine.  For some storage methods, you need a little glue.
  462
+
  463
+C<SDBM_File> doesn't supply an C<EXISTS> method, so included in this
  464
+package is a glue module called C<Memoize::SDBM_File> which does
  465
+provide one.  Use this instead of plain C<SDBM_File> to store your
  466
+cache table on disk in an C<SDBM_File> database:
  467
+
  468
+	memoize 'function', 
  469
+                SCALAR_CACHE => 
  470
+                [TIE, Memoize::SDBM_File, $filename, O_RDWR|O_CREAT, 0666];
  471
+
  472
+C<Storable> isn't a tied hash class at all.  You can use it to store a
  473
+hash to disk and retrieve it again, but yu can't modify the hash while
  474
+it's on the disk.  So if you want to store your cache table in a
  475
+C<Storable> database, use C<Memoize::Storable>, which puts a hashlike
  476
+front-end onto C<Storable>.  The hash table is actually kept in
  477
+memory, and is loaded from your C<Storable> file at the time you
  478
+memoize the function, and stored back at the time you unmemoize the
  479
+function:
  480
+
  481
+	memoize 'function', 
  482
+                SCALAR_CACHE => [TIE, Memoize::Storable, $filename];
456 483
 
457  
-=head1 OTHER PEOPLE'S BUGS
  484
+	memoize 'function', 
  485
+                SCALAR_CACHE => [TIE, Memoize::Storable, $filename, 'nstore'];
458 486
 
459  
-The tied hash class you use for storing your cache table must support
460  
-the following methods: C<tiehash>, C<fetch>, C<store>, C<exists>.  In
461  
-particular, you can't use C<SDBM_File> because it doesn't have
462  
-C<exists>.  This package contains a glue module, called
463  
-C<Memoize::SDBM_File>, which provides an C<exists> method so that you
464  
-can use C<SDBM_File> with C<Memoize>.  Just replace C<SDBM_File> with
465  
-C<Memoize::SDBM_File> in your call to C<memoize>; everything else is
466  
-the same.
  487
+Include the `nstore' option to have the C<Storable> database written
  488
+in `network order'.  (See L<Storable> for moer details about this.)
  489
+
  490
+=head1 MY BUGS
  491
+
  492
+Needs a better test suite, especially for the tied stuff.
  493
+That is why the version number is 0.46 instead of 0.50.
467 494
 
468 495
 =head1 MAILING LIST
469 496
 
@@ -547,7 +574,7 @@ sub memoize {
547 574
     $install_name = $uppack . '::' . $install_name
548 575
 	unless $install_name =~ /::/;
549 576
     no strict;
550  
-    local($) = 0;	       # ``Subroutine $install_name redefined at ...''
  577
+    local($^W) = 0;	       # ``Subroutine $install_name redefined at ...''
551 578
     *{$install_name} = $wrapper; # Install memoized version
552 579
   }
553 580
 
@@ -742,11 +769,25 @@ sub unmemoize {
742 769
   my $name = $tabent->{NAME};
743 770
   if (defined $name) {
744 771
     no strict;
745  
-    local($) = 0;	       # ``Subroutine $install_name redefined at ...''
  772
+    local($^W) = 0;	       # ``Subroutine $install_name redefined at ...''
746 773
     *{$name} = $tabent->{UNMEMOIZED}; # Replace with original function
747 774
   }
748 775
   undef $memotable{$revmemotable{$cref}};
749 776
   undef $revmemotable{$cref};
  777
+
  778
+  # This removes the last reference to the (possibly tied) memo tables
  779
+  # my ($old_function, $memotabs) = @{$tabent}{'UNMEMOIZED','MEMOS'};
  780
+  # undef $tabent; 
  781
+
  782
+#  # Untie the memo tables if they were tied.
  783
+#  my $i;
  784
+#  for $i (0,1) {
  785
+#    if (tied %{$memotabs->[$i]}) {
  786
+#      warn "Untying hash #$i\n";
  787
+#      untie %{$memotabs->[$i]};
  788
+#    }
  789
+#  }
  790
+
750 791
   $tabent->{UNMEMOIZED};
751 792
 }
752 793
 
61  Memoize/Storable.pm
... ...
@@ -0,0 +1,61 @@
  1
+
  2
+package Memoize::Storable;
  3
+use Storable ();
  4
+$Verbose = 0;
  5
+
  6
+sub TIEHASH {
  7
+  require Carp if $Verbose;
  8
+  my $package = shift;
  9
+  my $filename = shift;
  10
+  my $truehash = (-e $filename) ? Storable::retrieve($filename) : {};
  11
+  my %options;
  12
+  print STDERR "Memoize::Storable::TIEHASH($filename, @_)\n" if $Verbose;
  13
+  @options{@_} = ();
  14
+  my $self = 
  15
+    {FILENAME => $filename, 
  16
+     H => $truehash, 
  17
+     OPTIONS => \%options
  18
+    };
  19
+  bless $self => $package;
  20
+}
  21
+
  22
+sub STORE {
  23
+  require Carp if $Verbose;
  24
+  my $self = shift;
  25
+  print STDERR "Memoize::Storable::STORE(@_)\n" if $Verbose;
  26
+  $self->{H}{$_[0]} = $_[1];
  27
+}
  28
+
  29
+sub FETCH {
  30
+  require Carp if $Verbose;
  31
+  my $self = shift;
  32
+  print STDERR "Memoize::Storable::FETCH(@_)\n" if $Verbose;
  33
+  $self->{H}{$_[0]};
  34
+}
  35
+
  36
+sub EXISTS {
  37
+  require Carp if $Verbose;
  38
+  my $self = shift;
  39
+  print STDERR "Memoize::Storable::EXISTS(@_)\n" if $Verbose;
  40
+  exists $self->{H}{$_[0]};
  41
+}
  42
+
  43
+sub DESTROY {
  44
+  require Carp if $Verbose;
  45
+  my $self= shift;
  46
+  print STDERR "Memoize::Storable::DESTROY(@_)\n" if $Verbose;
  47
+  if ($self->{OPTIONS}{'nstore'}) {
  48
+    Storable::nstore($self->{H}, $self->{FILENAME});
  49
+  } else {
  50
+    Storable::store($self->{H}, $self->{FILENAME});
  51
+  }
  52
+}
  53
+
  54
+sub FIRSTKEY {
  55
+  'Fake hash from Memoize::Storable';
  56
+}
  57
+
  58
+sub NEXTKEY {
  59
+  undef;
  60
+}
  61
+1;
190  README
... ...
@@ -1,13 +1,22 @@
1 1
 
2 2
 Name:		Memoize
3 3
 What:		Transparently speed up functions by caching return values.
4  
-Version:	0.45
  4
+Version:	0.46
5 5
 Author:		Mark-Jason Dominus (mjd-perl-memoize@plover.com)
6 6
 
7 7
 ################################################################
8 8
 
9 9
 What's new since the previous release:
10 10
 
  11
+Now has an interface to `Storable'.  This wasn't formerly possible,
  12
+because the base package can only store caches via modules that
  13
+present a tied hash interface, and `Storable' doesn't.  Solution:
  14
+Memoize::Storable is a tied hash interface to `Storable'.
  15
+
  16
+################################################################
  17
+
  18
+What's new since 0.45:
  19
+
11 20
 Storage of cached function return values in a static file is now
12 21
 tentatively supported.  `memoize' now accepts new options SCALAR_CACHE
13 22
 and LIST_CACHE to specify the destination and protocol for saving
@@ -44,7 +53,6 @@ do so.
44 53
 
45 54
 ################################################################
46 55
 
47  
-
48 56
 =head1 NAME
49 57
 
50 58
 Memoize - Make your functions faster by trading space for time
@@ -160,13 +168,14 @@ There are some optional options you can pass to C<memoize> to change
160 168
 the way it behaves a little.  To supply options, invoke C<memoize>
161 169
 like this:
162 170
 
163  
-	memoize(function, TODISK => filename,
164  
-	                  NORMALIZER => function,
165  
-			  INSTALL => newname
  171
+	memoize(function, NORMALIZER => function,
  172
+			  INSTALL => newname,
  173
+                          SCALAR_CACHE => option,
  174
+	                  LIST_CACHE => option
166 175
 			 );
167 176
 
168  
-Each of these three options is optional; you can include some, all, or
169  
-none of them.
  177
+Each of these options is optional; you can include some, all, or none
  178
+of them.
170 179
 
171 180
 =head2 INSTALL
172 181
 
@@ -180,6 +189,9 @@ installs the memoized version of C<fib> as C<fastfib>; without the
180 189
 C<INSTALL> option it would have replaced the old C<fib> with the
181 190
 memoized version.  
182 191
 
  192
+To prevent C<memoize> from installing the memoized version anywhere, use
  193
+C<INSTALL => undef>.
  194
+
183 195
 =head2 NORMALIZER
184 196
 
185 197
 Suppose your function looks like this:
@@ -233,7 +245,7 @@ You would tell C<Memoize> to use this normalizer this way:
233 245
 C<memoize> knows that if the normalized version of the arguments is
234 246
 the same for two argument lists, then it can safely look up the value
235 247
 that it computed for one argument list and return it as the result of
236  
-calling the function with the other argmuent list, even if the
  248
+calling the function with the other argument list, even if the
237 249
 argument lists look different.
238 250
 
239 251
 The default normalizer just concatenates the arguments with C<$;> in
@@ -255,18 +267,135 @@ would in scalar context, you can have the normalizer function select
255 267
 its behavior based on the results of C<wantarray>.  Even if called in
256 268
 a list context, a normalizer should still return a single string.
257 269
 
258  
-=head2 TODISK
  270
+=head2 C<SCALAR_CACHE>, C<LIST_CACHE>
  271
+
  272
+Normally, C<Memoize> caches your function's return values into an
  273
+ordinary Perl hash variable.  However, you might like to have the
  274
+values cached on the disk, so that they persist from one run of your
  275
+program to the next, or you might like to associate some other
  276
+interesting semantics with the cached values.  
  277
+
  278
+There's a slight complication under the hood of C<Memoize>: There are
  279
+actually I<two> caches, one for scalar values and one for list values.
  280
+When your function is called in scalar context, its return value is
  281
+cached in one hash, and when your function is called in list context,
  282
+its value is cached in the other hash.  You can control the caching
  283
+behavior of both contexts independently with these options.
  284
+
  285
+The argument to C<LIST_CACHE> or C<SCALAR_CACHE> must either be one of
  286
+the following four strings:
  287
+
  288
+	MEMORY
  289
+	TIE
  290
+	FAULT
  291
+	MERGE
  292
+
  293
+or else it must be a reference to a list whose first element is one of
  294
+these four strings, such as C<[TIE, arguments...]>.
  295
+
  296
+=over 4
  297
+
  298
+=item C<MEMORY>
  299
+
  300
+C<MEMORY> means that return values from the function will be cached in
  301
+an ordinary Perl hash variable.  The hash variable will not persist
  302
+after the program exits.  This is the default.
  303
+
  304
+=item C<TIE>
  305
+
  306
+C<TIE> means that the function's return values will be cached in a
  307
+tied hash.  A tied hash can have any semantics at all.  It is
  308
+typically tied to an on-disk database, so that cached values are
  309
+stored in the database and retrieved from it again when needed, and
  310
+the disk file typically persists after your pogram has exited.  
  311
+
  312
+If C<TIE> is specified as the first element of a list, the remaining
  313
+list elements are taken as arguments to the C<tie> call that sets up
  314
+the tied hash.  For example,
  315
+
  316
+	SCALAR_CACHE => [TIE, DB_File, $filename, O_RDWR | O_CREAT, 0666]
  317
+
  318
+says to tie the hash into the C<DB_File> package, and to pass the
  319
+C<$filename>, C<O_RDWR | O_CREAT>, and C<0666> arguments to the C<tie>
  320
+call.  This has the effect of storing the cache in a C<DB_File>
  321
+database whose name is in C<$filename>.
  322
+
  323
+Other typical uses of C<TIE>:
  324
+
  325
+	LIST_CACHE => [TIE, GDBM_File, $filename, O_RDWR | O_CREAT, 0666]
  326
+	SCALAR_CACHE => [TIE, MLDBM, DB_File, $filename, O_RDWR|O_CREAT, 0666]
  327
+	LIST_CACHE => [TIE, My_Package, $tablename, $key_field, $val_field]
  328
+
  329
+This last might tie the cache hash to a package that you wrote
  330
+yourself that stores the cache in a SQL-accessible database.
  331
+A useful use of this feature: You can construct a batch program that
  332
+runs in the background and populates the memo table, and then when you
  333
+come to run your real program the memoized function will be
  334
+screamingly fast because all its results have been precomputed. 
259 335
 
260  
-C<TODISK> means that the memo table should be saved to disk so that it
261  
-will persist between invocations of your program.  If you use this
262  
-option, future runs of your program will get immediate benefit from
263  
-the results computed by earlier runs.  A useful use of this feature:
264  
-You can construct a batch program that runs in the background and
265  
-populates the memo table, and then when you come to run your real
266  
-program the memoized function will be screamingly fast because al lits
267  
-results have been precomputed.  Or you would be able to do this, if
268  
-TODISK were implemented, which it presently isn't.  But it will be.
269  
-Some day.  
  336
+=item C<FAULT>
  337
+
  338
+C<FAULT> means that you never expect to call the function in scalar
  339
+(or list) context, and that if C<Memoize> detects such a call, it
  340
+should abort the program.  The error message is one of
  341
+
  342
+	`foo' function called in forbidden list context at line ...
  343
+	`foo' function called in forbidden scalar context at line ...
  344
+
  345
+=item C<MERGE>
  346
+
  347
+C<MERGE> normally means the function does not distinguish between list
  348
+and sclar context, and that return values in both contexts should be
  349
+stored together.  C<LIST_CACHE =E<gt> MERGE> means that list context
  350
+return values should be stored in the same hash that is used for
  351
+scalar context returns, and C<SCALAR_CACHE =E<gt> MERGE> means the
  352
+same, mutatis mutandis.  It is an error to specify C<MERGE> for both,
  353
+but it probably does something useful.
  354
+
  355
+Consider this function:
  356
+
  357
+	sub pi { 3; }
  358
+
  359
+Normally, the following code will result in two calls to C<pi>:
  360
+
  361
+    $x = pi();
  362
+    ($y) = pi();
  363
+    $z = pi();
  364
+
  365
+The first call caches the value C<3> in the scalar cache; the second
  366
+caches the list C<(3)> in the list cache.  The third call doesn't call
  367
+the real C<pi> function; it gets the value from the scalar cache.
  368
+
  369
+Obviously, the second call to C<pi> is a waste of time, and storing
  370
+its return value is a waste of space.  Specifying C<LIST_CACHE
  371
+=E<gt> MERGE> will make C<memoize> use the same cache for scalar and
  372
+list context return values, so that the second call uses the scalar
  373
+cache that was populated by the first call.  C<pi> ends up being
  374
+cvalled only once, and both subsequent calls return C<3> from the
  375
+cache, regardless of the calling context.
  376
+
  377
+Another use for C<MERGE> is when you want both kinds of return values
  378
+stored in the same disk file; this saves you from having to deal with
  379
+two disk files instead of one.  You can use a normalizer function to
  380
+keep the two sets of return values separate.  For example:
  381
+
  382
+	memoize 'myfunc',
  383
+	  NORMALIZER => 'n',
  384
+	  SCALAR_CACHE => [TIE, MLDBM, DB_File, $filename, ...],
  385
+	  LIST_CACHE => MERGE,
  386
+	;
  387
+
  388
+	sub n {
  389
+	  my $context = wantarray() ? 'L' : 'S';
  390
+	  # ... now compute the hash key from the arguments ...
  391
+	  $hashkey = "$context:$hashkey";
  392
+	}
  393
+
  394
+This normalizer function will store scalar context return values in
  395
+the disk file under keys that begin with C<S:>, and list context
  396
+return values under keys that begin with C<L:>.
  397
+
  398
+=back
270 399
 
271 400
 =head1 OTHER FUNCTION
272 401
 
@@ -360,6 +489,26 @@ and shorter every time you call C<main>.
360 489
 
361 490
 =back
362 491
 
  492
+=head1 MY BUGS
  493
+
  494
+Needs a better test suite, especially for the tied stuff.
  495
+That is why the version number is 0.45 instead of 0.50.
  496
+
  497
+=head1 OTHER PEOPLE'S BUGS
  498
+
  499
+The tied hash class you use for storing your cache table must support
  500
+the following methods: C<tiehash>, C<fetch>, C<store>, C<exists>.  In
  501
+particular, you can't use C<SDBM_File> because it doesn't have
  502
+C<exists>.  This package contains a glue module, called
  503
+C<Memoize::SDBM_File>, which provides an C<exists> method so that you
  504
+can use C<SDBM_File> with C<Memoize>.  Just replace C<SDBM_File> with
  505
+C<Memoize::SDBM_File> in your call to C<memoize>; everything else is
  506
+the same.
  507
+
  508
+=head1 MAILING LIST
  509
+
  510
+To join a very low-traffic mailing list for announcements about
  511
+C<Memoize>, send an empty note to C<mjd-perl-memoize-request@plover.com>.
363 512
 
364 513
 =head1 AUTHOR
365 514
 
@@ -376,5 +525,8 @@ for news and upgrades.
376 525
 
377 526
 =end html
378 527
 
  528
+To join a mailing list for announcements about C<Memoize>, send an
  529
+empty message to C<mjd-perl-memoize-request@plover.com>.
  530
+
379 531
 =cut
380 532
 
98  TODO
... ...
@@ -1,4 +1,4 @@
1  
-# Version 0.05 alpha $Revision: 1.3 $ $Date: 1998/09/05 03:44:04 $
  1
+# Version 0.05 alpha $Revision: 1.4 $ $Date: 1998/09/07 19:48:43 $
2 2
 
3 3
 =head1 TO DO
4 4
 
@@ -23,10 +23,7 @@ large effect on the performance of C<main>.  But if there was a big
23 23
 difference, you would know that C<suba> or C<subb> was a good
24 24
 candidate for optimization if you needed to make C<main> go faster.
25 25
 
26  
-=item * 
27  
-
28  
-Maybe a tied-hash interface to the memo-table, which a hook to
29  
-      automatically populate an entry if no value is there yet?
  26
+Done.
30 27
 
31 28
 =item * 
32 29
 
@@ -39,6 +36,8 @@ We could even bless the new function reference so that it could have
39 36
 accessor methods for getting to the original function, the options,
40 37
 the memo table, etc.
41 38
 
  39
+Naah.
  40
+
42 41
 =item *
43 42
 
44 43
 The TODISK feature is not ready yet.  It will have to be rather
@@ -46,6 +45,8 @@ complicated, providing options for which disk method to use (GDBM?
46 45
 DB_File?  Flat file?  Storable?  User-supplied?) and which stringizing
47 46
 method to use (FreezeThaw?  Marshal?  User-supplied?)
48 47
 
  48
+Done!
  49
+
49 50
 =item *
50 51
 
51 52
 Maybe an option for automatic expiration of cache values?  (`After one
@@ -76,9 +77,13 @@ reason not to do this, but I can't remember what it was.
76 77
 
77 78
 Add more array value tests to the test suite.
78 79
 
  80
+Does it need more now?
  81
+
79 82
 =item *
80 83
 
81  
-Fix that `Subroutine u rededined ... line 484' message.
  84
+Fix that `Subroutine u redefined ... line 484' message.
  85
+
  86
+Fixed, I think.
82 87
 
83 88
 =item *
84 89
 
@@ -89,54 +94,9 @@ Get rid of any remaining *{$ref}{CODE} or similar magic hashes.
89 94
 There should be an option to dump out the memoized values or to
90 95
 otherwise traverse them.
91 96
 
92  
-=item *
93  
-
94  
-There was probably some other stuff that I forgot.
95  
-
96  
-
97  
-=item * 
98  
-
99  
-Here's the preliminary interface spec for the C<TODISK> option:
100  
-`memoize' takes options named C<SCALAR_CONTEXT> and C<LIST_CONTEXT>.
101  
-Legal values are
102  
-
103  
-	MEMORY
104  
-	TIE
105  
-	FAULT
106  
-	MERGE
107  
-
108  
-or a reference to a list whose first element is one of these.  The
109  
-default for both is MEMORY, which means that Perl's builtin hashes are
110  
-used, the way they are now.  FAULT means that the function should
111  
-never be called in scalar/list context, and that Memoize should croak
112  
-if it is.  TIE means that the hash will be tied; it's usaully written
113  
-as
114  
-
115  
-	[TIE, packagename, argument-list]
116  
-
117  
-which specifies the package name and arguments for the tie.  Memoize
118  
-will load the package if appropriate.  Thus
119  
-
120  
-	[TIE, Storable, filename, ...]
121  
-	[TIE, DB_File, filename, flags, mode, ...]
122  
-	[TIE, MLDBM, DB_File, ... ]
123  
-
124  
-MERGE means that return values in the specified context will be stored
125  
-in the same structure that is used for the other context.  For
126  
-example, suppose you have a function which always returns a scalar,
127  
-and doesn't care whether it was called in scalar or list context.  You
128  
-don't want to store its list-context reutrn separately from its
129  
-scalar-context return, because they're going to be the same anyway,
130  
-and if you stored them separately, you'd waste a call and a cache
131  
-slot.  So you say LIST_CONTEXT => MERGE, and then list context is
132  
-considered the same as scalar context.
133  
-
134  
-You can also use MERGE with a normalizer to get the list-context and
135  
-scalar-context returns stored in the same database without conflicting
136  
-with each other.
  97
+What for?
137 98
 
138  
-If you specify MERGE for both, it's either an error or else you get
139  
-them stored in one in-memory hash, or something.
  99
+Maybe the tied hash interface taskes care of this anyway?
140 100
 
141 101
 =item *
142 102
 
@@ -146,25 +106,19 @@ Include an example that caches DNS lookups.
146 106
 
147 107
 Make tie for Storable (Memoize::Storable)
148 108
 
149  
-=item*
  109
+A prototype of Memoize::Storable is finished.  Test it and add to the
  110
+test suite.
150 111
 
151  
-Make tie for DBI  (Memoize::DBI)
  112
+Done.
152 113
 
153 114
 =item *
154 115
 
155  
-Tie for SDBM doesn't work.  Can't subclass SDBM?  Why not?
  116
+Make tie for DBI  (Memoize::DBI)
156 117
 
157 118
 =item *
158 119
 
159 120
 I think there's a bug.  See `###BUG'.
160 121
 
161  
-=item *
162  
-
163  
-Docs / code inconsistent about SCALAR_CONTEXT vs SCALAR_CACHE.
164  
-Make up your mind.
165  
-
166  
-Decision:  SCALAR_CACHE.
167  
-
168 122
 =item * 
169 123
 
170 124
 Storable probably can't be done, because it doesn't allow updating.
@@ -187,11 +141,29 @@ writable in-memory cache?  A generic tied hash maybe?
187 141
 
188 142
 Maybe `save' and `restore' methods?
189 143
 
  144
+It isn't working right because the destructor doesn't get called at
  145
+the right time.
  146
+
  147
+This is fixed.  `use strict vars' would have caught it immediately.  Duh.
  148
+
190 149
 =item *
191 150
 
  151
+Don't forget about generic interface to Storable-like packages
  152
+
  153
+=item * 
  154
+
  155
+
192 156
 Maybe add in TODISK after all, with TODISK => 'filename' equivalent to
193 157
 
194 158
 	SCALAR_CACHE => [TIE, Memoize::SDBM_File, $filename, O_RDWR|O_CREAT, 0666],
195 159
 	LIST_CACHE => MERGE
196 160
 
  161
+=item *
  162
+
  163
+Maybe the default for LIST_CACHE should be MERGE anyway.
  164
+
  165
+=item *
  166
+
  167
+There was probably some other stuff that I forgot.
  168
+
197 169
 =back
6  t/speed.t
@@ -3,8 +3,14 @@
3 3
 use lib '..';
4 4
 use Memoize;
5 5
 
  6
+if (-e '.fast') {
  7
+  print "1..0\n";
  8
+  exit 0;
  9
+}
  10
+
6 11
 print STDERR "\nWarning: I'm testing the speedup.  This might take up to sixty seconds.\n                    ";
7 12
 
  13
+
8 14
 print "1..6\n";
9 15
 
10 16
 sub fib {
69  t/tie_storable.t
... ...
@@ -0,0 +1,69 @@
  1
+#!/usr/bin/perl
  2
+# -*- mode: perl; perl-indent-level: 2 -*-
  3
+
  4
+use lib qw(. ..);
  5
+use Memoize 0.45 qw(memoize unmemoize);
  6
+use Memoize::Storable;
  7
+$Memoize::Storable::Verbose = 0;
  8
+
  9
+sub i {
  10
+  $_[0];
  11
+}
  12
+
  13
+sub c119 { 119 }
  14
+sub c7 { 7 }
  15
+sub c43 { 43 }
  16
+sub c23 { 23 }
  17
+sub c5 { 5 }
  18
+
  19
+sub n {
  20
+  $_[0]+1;
  21
+}
  22
+
  23
+eval {require Storable};
  24
+if ($@) {
  25
+  print "1..0\n";
  26
+  exit 0;
  27
+}
  28
+
  29
+print "1..4\n";
  30
+
  31
+$file = '/tmp/storable.db';
  32
+unlink $file;
  33
+tryout('Memoize::Storable', $file, 1);  # Test 1..4
  34
+unlink $file;
  35
+
  36
+sub tryout {
  37
+  my ($tiepack, $file, $testno) = @_;
  38
+
  39
+
  40
+  memoize 'c5', 
  41
+  SCALAR_CACHE => ['TIE', $tiepack, $file], 
  42
+  LIST_CACHE => 'FAULT'
  43
+    ;
  44
+
  45
+  my $t1 = c5();	
  46
+  my $t2 = c5();	
  47
+  print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n");
  48
+  $testno++;
  49
+  print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n");
  50
+  unmemoize 'c5';
  51
+  1;
  52
+  1;
  53
+
  54
+  # Now something tricky---we'll memoize c23 with the wrong table that
  55
+  # has the 5 already cached.
  56
+  memoize 'c23', 
  57
+  SCALAR_CACHE => ['TIE', $tiepack, $file], 
  58
+  LIST_CACHE => 'FAULT'
  59
+    ;
  60
+  
  61
+  my $t3 = c23();
  62
+  my $t4 = c23();
  63
+  $testno++;
  64
+  print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n");
  65
+  $testno++;
  66
+  print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n");
  67
+  unmemoize 'c23';
  68
+}
  69
+

0 notes on commit 84a67d2

Please sign in to comment.
Something went wrong with that request. Please try again.