Help language development. Donate to The Perl Foundation

Sub::Memoized cpan:ELIZABETH last updated on 2020-10-30

Sub::Memoized - trait for memoizing calls to subroutines


    use Sub::Memoized;

    sub a($a,$b) is memoized {
        # do some expensive calculation

    sub b($a, $b) is memoized( my %cache ) {
        # do some expensive calculation with direct access to cache

    use Hash::LRU;
    sub c($a, $b) is memoized( my %cache is LRU( elements => 2048 ) ) {
        # do some expensive calculation, keep last 2048 results returned


Sub::Memoized provides a `is memoized` trait on `Sub`routines as an easy way to cache calculations made by that subroutine (assuming a given set of input parameters will always produce the same result).

Optionally, you can specify a hash that will serve as the cache. This allows later access to the generated results. Or you can specify a specially crafted hash, such as one made with `Hash::LRU`.


Elizabeth Mattijsen <[email protected]>

Source can be located at: . Comments and Pull Requests are welcome.


Copyright 2018,2020 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.