UPDATE: Code fixed below.
The problem was the calls to function() are called
in list context, therefore Memoize stores it in
a list cache, separate from the scalar cache.
Adding an empty string before the function call
forces a scalar context.
The segfault went away when I added an unmemoize.
But on the second run, it calls the function again
instead of using the supposedly cached data in memory.tmp.
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Memoize qw(memoize unmemoize);
use Memoize::Storable;
my $filename = './memory.tmp';
tie my %cache => 'Memoize::Storable', $filename;
memoize 'function', SCALAR_CACHE => [HASH => \%cache];
print "".function(1), "\n";
print "".function(1), "\n";
print "".function(2), "\n"; # Changed original order
print "".function(1), "\n";
unmemoize 'function';
sub function {
my $wtf = shift;
print "I am the $wtf function\n";
return "anything $wtf";
}
First run results:
I am the 1 function
anything 1
anything 1
I am the 2 function
anything 2
anything 1
Second run results:
anything 1
anything 1
anything 2
anything 1
|