Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/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' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Memoize problem with old Perl
by choroba (Cardinal) on Sep 21, 2018 at 09:10 UTC |