vishnuvp
vishnuvp

Reputation: 283

Change Perl Memoize Cache during runtime

I have a method that reads a flag from a file and it is memoized.

use Memoize;

my %cache;
memoize('readFlag',  SCALAR_CACHE => [HASH => \%cache]);
sub readFlag {
 # read flag & return
}

Memoize works fine and I get the cached value always. Now, I want to modify this cache entry during runtime and any running script to pick up the change. Tried using a pointer to the method, however that just creates another entry in the hashmap and sets the value but does not update the existing value.

$cache{\readFlag} = newVal;

Can someone help me out?

Upvotes: 0

Views: 140

Answers (1)

ikegami
ikegami

Reputation: 386551

The cache would be keyed on parameters (not whatever you think \readFlag does).

Specifically,

The default normalizer just concatenates the arguments with character 28 in between

So if you wanted to make it so readFlag("a", "b") returns c, you would use

$cache{ join chr(28), "a", "b" } = "c";

Note that this won't affect "any running script", just this process. The cache only exists in the process. You can take steps to make the cache persistent, but you didn't show anything of the kind.

Upvotes: 1

Related Questions