Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Memoize problem with old Perl

by Anonymous Monk
on Sep 21, 2018 at 03:28 UTC ( [id://1222781]=perlquestion: print w/replies, xml ) Need Help??

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
    Weird, works for me:
    Perl 5.018002 Memoize 1.03 Tempdir /tmp Tempfile 5.018002_memoize_fib Filesize 12288 bytes Question 8 Answer 21 $%cache = { '0' => '0', '2' => '1', '4' => '3', '6' => '8', '1' => '1', '3' => '2', '5' => '5', '7' => '13' };
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

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

    No recent polls found