I'm not sure the bug has a good fix.

There's a method compute which takes a key and a callback parameter. If there's no value for the given key, the callback is used to create the value and stored in the cache.

The problem is the method is context sensitive, i.e. it uses wantarray, propagates the context to the callback, and stores its return value in a different way based on the context.

But it's perfectly fine to store a value in a cache in scalar context and later retrieve it in list context - and the current implementation might fail, because it tries to apply array dereference in list context. Moreover, if the callback is context sensitive and returns something else in scalar versus list context, you never know which value is stored in the cache - it depends on which context you first called compute in.

What should we do? Calling the callback twice to store both the values is crazy (and might break code if it uses the callback to e.g. generate a unique id).

If it was upon me to decide, I'd remove context sensitivity from the method. If you want to store an array reference, provide a callback that does so.

Just for illustration, here's a test that passes with the current implementation (suitable to be pasted into lib/CHI/t/Bugs.pm):

sub test_141024 : Tests { my $cache = CHI->new( driver => 'RawMemory', global => 1 ); my $callback = sub { wantarray ? 'list' : 'scalar' }; my $scalar = $cache->compute( 1, undef, $callback ); my @array = $cache->compute( 2, undef, $callback ); is_deeply( $cache->get(1), 'scalar' ); is_deeply( $cache->get(2), ['list'] ); eval { my @array2 = $cache->compute( 1, undef, $callback ) }; like( $@, qr/Can't use string \("scalar"\) as an ARRAY ref while " +strict refs"/ ); $cache->set( 3, ['in_list'] ); is_deeply( $cache->get(3), ['in_list'] ); my @foo = map $cache->compute( $_, undef, $callback ), 3, 4; is_deeply( $foo[0], 'in_list' ); is_deeply( $foo[1], 'list' ); }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re^6: Big cache -- CHI Unified cache handling interface by choroba
in thread Big cache by Liebranca

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.