ysth has asked for the wisdom of the Perl Monks concerning the following question:
(If you have it enabled, //= is even better.)my %foo_cache; sub foo { $foo_cache{join ",", @_} ||= do { ... rest of sub goes here } }
But what about when the function generates a list? Easy enough:
But that changes the result in scalar context if an actual list is returned. The fix is a little expensive:my %foo_cache; sub foo { @{$foo_cache{join ",", @_} ||= [ do { ... rest of sub goes here }]} }
Can anybody suggest a less expensive alternative? Pity there's no list keyword.my %foo_cache; sub foo { return @$_[0..$#$_] for $foo_cache{join ",", @_} ||= [ do { ... rest of sub goes here }] }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: inline caching a list-generating sub
by Roy Johnson (Monsignor) on Aug 19, 2005 at 11:58 UTC | |
|
Re: inline caching a list-generating sub
by Roger (Parson) on Aug 19, 2005 at 12:03 UTC | |
by Roy Johnson (Monsignor) on Aug 19, 2005 at 14:36 UTC | |
|
Re: inline caching a list-generating sub
by eric256 (Parson) on Aug 19, 2005 at 15:27 UTC | |
by ysth (Canon) on Aug 19, 2005 at 18:04 UTC | |
by etm117 (Pilgrim) on Aug 19, 2005 at 16:36 UTC | |
|
Re: inline caching a list-generating sub (clarity)
by tye (Sage) on Aug 19, 2005 at 21:52 UTC |