Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Memoize::Expire oddity

by Random_Walk (Prior)
on Aug 14, 2013 at 14:18 UTC ( [id://1049426]=perlquestion: print w/replies, xml ) Need Help??

Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

I was just playing around with Memoize::Expire and did a one line test. It worked as expected when I use "print f().$/" but does not expire the memoized function f() when I use "say f();"

perl -MMemoize -MMemoize::Expire -E "sub f {int rand 100}; tie my %cache => 'Memoize::Expire',LIFETIME=>2; memoize 'f',SCALAR_CACHE => [HASH => \%cache ]; for (1..10) {print f().$/; sleep 1}" 16 16 95 95 61 61 62 62 93 93 perl -MMemoize -MMemoize::Expire -E "sub f {int rand 100}; tie my %cache => 'Memoize::Expire',LIFETIME=>2; memoize 'f',SCALAR_CACHE => [HASH => \%cache ]; for (1..10) {say f(); sleep 1}" 89 89 89 89 89 89 89 89 89 89

After a bit of tracking down I found it only works when the function is called in scalar context. For instance say f()+0; works fine.

I tried altering my invocation of memoize to use LIST_CACHE rather than SCALAR_CACHE but then it crashes

Can't use string ("ARRAY(0x9c7494)") as an ARRAY ref while "strict refs" in use at C:/Perl516/lib/Memoize.pm line 258.

Does anyone know if Memoize::Expire can be got to work in list context? Or do I have to roll my own if I need that

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: Memoize::Expire oddity
by McA (Priest) on Aug 14, 2013 at 15:47 UTC

    Hi,

    looking at the code of Memoize::Expire on line 56 of the current version

    $self->{C}{$key} = $header . $value;
    let me assume that there is a bug or it was never intended to use this module in list context. In your case $value is an array ref, which gets stringified by this operation. When fetching this value you get the error you presented.

    I'm sorry, but in this case I can only cite the man page to this module

    This module is experimental, and may contain bugs. Please report bugs to the address below.

    Best regards
    McA

Re: Memoize::Expire oddity
by ryanc (Monk) on Aug 15, 2013 at 17:51 UTC
    My first guess was to add LIST_CACHE => 'MERGE' to your memoize options. I tested it this way and your second example seems to work now. I didn't make any other changes.

    I'm running Perl 5.14.2.

      I tried MERGE too and got the same error. I was using 5.16.2 on Windows. That's my work box. I'm home now on Linux, MERGE is also not helping me. am I using the option wrongly?

      r@Che ~ $ perl -MMemoize -MMemoize::Expire -E "sub f {int rand 100}; \ tie my %cache => 'Memoize::Expire',LIFETIME=>2,LIST_CACHE=>' +MERGE';\ memoize 'f',SCALAR_CACHE => [HASH => \%cache ];\ for (1..10) {say f(); sleep 1}" 45 45 45 45 45 45 45 45 45 45 r@Che ~ $ perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-li +nux-gnu-thread-multi (with 65 registered patches, see perl -V for more detail) Copyright 1987-2011, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
        $ perl -MMemoize -MMemoize::Expire -E " \ sub f { int rand 100 }; \ tie my %cache => 'Memoize::Expire', LIFETIME => 2; \ memoize 'f', LIST_CACHE => 'MERGE', \ SCALAR_CACHE => [ HASH => \%cache ]; \ for (1..10) { say f(); sleep 1 }" 62 62 56 56 39 39 85 85 42

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1049426]
Approved by marto
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-23 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found