I was reading HOP and it got me thinking about inline caching methods. Assuming the no-false-values constraint is not a problem, there's an easy and efficient way to add caching to a sub that returns a scalar (replace the join as needed):
my %foo_cache; sub foo { $foo_cache{join ",", @_} ||= do { ... rest of sub goes here } }
(If you have it enabled, //= is even better.)

But what about when the function generates a list? Easy enough:

my %foo_cache; sub foo { @{$foo_cache{join ",", @_} ||= [ do { ... rest of sub goes here }]} }
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 { return @$_[0..$#$_] for $foo_cache{join ",", @_} ||= [ do { ... rest of sub goes here }] }
Can anybody suggest a less expensive alternative? Pity there's no list keyword.

In reply to inline caching a list-generating sub by ysth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.