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 undernameinstead;INSTALL => undefinstalls it nowhere.NORMALIZER => 'name'or=> \&codecomputes 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 withchr(28), sof(1, 2)andf("1", "2")share an entry and references are keyed by address.SCALAR_CACHEandLIST_CACHEchoose 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),FAULTto croak when called in that context, andMERGEto share one cache between the two contexts.TIEis 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.