#!/usr/bin/perl # SSCCE from examples at "perldoc Memoize" # Memoize works as expected on recent Perls 5.20-28. # Builds huge cache and does not work on 5.18. # Thoughts on why, and how to handle it? use strict; use warnings; use diagnostics; use Data::Dumper; use DB_File; use File::Spec; use integer; use Memoize; use POSIX (); my $title = 'Compute Fibonacci numbers'; my $tempd = File::Spec->tmpdir; my $tempf = $] . '_memoize_fib'; my $cache = File::Spec->catfile($tempd,$tempf); my $ARGS = @ARGV ? shift : 8; unlink $cache if -e $cache and $ARGS eq 'purge'; my $db = tie my %cache => 'DB_File', $cache, POSIX::O_RDWR|POSIX::O_CR +EAT, 0666; memoize 'fib', SCALAR_CACHE => [HASH => \%cache]; print "Perl $] Memoize $Memoize::VERSION Tempdir $tempd Tempfile $tempf Filesize ", -s $cache, " bytes Question $ARGS Answer ", fib($ARGS), "\n\n", # Because Data::Dumper panics on multigig dumps with # panic: sv_setpvn called with negative strlen -8260 eval { Data::Dumper->Dump([\%cache],[('%cache')]) }; unlink $cache if $@; print "$title\nUsage: $0 n, $0 purge\n"; sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); }

Perl 5.018002 Memoize 1.03 Tempdir /tmp Tempfile 5.018002_memoize_fib Filesize 146800640 bytes Question 8 Answer 21 $%cache = { '0' => undef, '1' => undef, '2' => undef, '3' => undef, '4' => undef, '5' => undef, '6' => undef, '7' => undef };
Perl 5.020003 Memoize 1.03 Tempdir /tmp Tempfile 5.020003_memoize_fib Filesize 49152 bytes Question 8 Answer 21 $%cache = { '0' => '0', '2' => '1', '4' => '3', '6' => '8', '1' => '1', '3' => '2', '5' => '5', '7' => '13' };
Perl 5.028000 Memoize 1.03_01 Tempdir /tmp Tempfile 5.028000_memoize_fib Filesize 49152 bytes Question 8 Answer 21 $%cache = { '0' => '0', '2' => '1', '4' => '3', '6' => '8', '1' => '1', '3' => '2', '5' => '5', '7' => '13' };

In reply to Memoize problem with old Perl by Anonymous Monk

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.