```{index} single: memoize (Memoize) ``` ```{index} single: Memoize::memoize ``` # memoize Memoize a function: `memoize('name', %options)` or `memoize(\&code, %options)`. Returns a code reference to the memoized wrapper. Given a name, the wrapper is also installed under that name (qualified with the caller's package unless it contains `::`), so every existing caller now goes through the cache; `\&name` changes accordingly. Given a code reference, nothing is installed unless `INSTALL` says so. Options: - `INSTALL => 'name'` installs the wrapper under `name` instead; `INSTALL => undef` installs it nowhere. - `NORMALIZER => 'name'` or `=> \&code` computes the cache key from the argument list (called in the same context as the wrapper; in list context its first return value is the key). Without it the key is the arguments joined with `chr(28)`, so `f(1, 2)` and `f("1", "2")` share an entry and references are keyed by address. - `SCALAR_CACHE` and `LIST_CACHE` choose the storage for results computed in scalar and list context: `MEMORY` (the default), `[HASH => \%hash]` to use your hash (tie it to a DBM file for a persistent cache), `FAULT` to croak when called in that context, and `MERGE` to share one cache between the two contexts. `TIE` is accepted for compatibility and warns that it is deprecated. Croaks `Cannot operate on nonexistent function` for a name that does not name a defined sub, and `Usage: memoize 'functionname'|coderef {OPTIONS}` for anything that is neither a name nor a plain code reference. A `die` inside the function propagates and caches nothing. A recursive function benefits fully: its recursive calls reach the cache too, so `fib(50)` becomes linear. Only memoize functions whose result depends solely on their arguments and that have no side effects - that is your assertion, not something the module checks.