File: Memoize.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (1002 lines) | stat: -rw-r--r-- 72,560 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Memoize - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>Memoize</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-modules-M.html">Core modules (M)</a> &gt;
      
    
    Memoize
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>Memoize</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#DETAILS">DETAILS</a><li><a href="#OPTIONS">OPTIONS</a><ul><li><a href="#INSTALL">INSTALL</a><li><a href="#NORMALIZER">NORMALIZER</a><li><a href="#SCALAR_CACHE%2c-LIST_CACHE">SCALAR_CACHE, LIST_CACHE</a></ul><li><a href="#OTHER-FACILITIES">OTHER FACILITIES</a><ul><li><a href="#unmemoize">unmemoize</a><li><a href="#flush_cache">flush_cache</a></ul><li><a href="#CAVEATS">CAVEATS</a><li><a href="#PERSISTENT-CACHE-SUPPORT">PERSISTENT CACHE SUPPORT</a><li><a href="#EXPIRATION-SUPPORT">EXPIRATION SUPPORT</a><li><a href="#BUGS">BUGS</a><li><a href="#MAILING-LIST">MAILING LIST</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#COPYRIGHT-AND-LICENSE">COPYRIGHT AND LICENSE</a><li><a href="#THANK-YOU">THANK YOU</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>Memoize - Make functions faster by trading space for time</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>        <span class="c"># This is the documentation for Memoize 1.03</span></li><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">Memoize</span><span class="sc">;</span></li><li>	<span class="i">memoize</span><span class="s">(</span><span class="q">&#39;slow_function&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">slow_function</span><span class="s">(</span><span class="w">arguments</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># Is faster than it was before</span></li></ol></pre><p>This is normally all you need to know.  However, many options are available:</p>
<pre class="verbatim"><ol><li>	<span class="i">memoize</span><span class="s">(</span><span class="w">function</span><span class="cm">,</span> <span class="w">options</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Options include:</p>
<pre class="verbatim"><ol><li>	<span class="w">NORMALIZER</span> <span class="cm">=&gt;</span> <span class="w">function</span></li><li>	<span class="w">INSTALL</span> <span class="cm">=&gt;</span> <span class="w">new_name</span></li><li></li><li>	<span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MEMORY&#39;</span></li><li>        <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="q">&#39;HASH&#39;</span><span class="cm">,</span> \<span class="i">%cache_hash</span> <span class="s">]</span></li><li>	<span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;FAULT&#39;</span></li><li>	<span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MERGE&#39;</span></li><li></li><li>	<span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MEMORY&#39;</span></li><li>        <span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="q">&#39;HASH&#39;</span><span class="cm">,</span> \<span class="i">%cache_hash</span> <span class="s">]</span></li><li>	<span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;FAULT&#39;</span></li><li>	<span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MERGE&#39;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>`Memoizing' a function makes it faster by trading space for time.  It
does this by caching the return values of the function in a table.
If you call the function again with the same arguments, <code class="inline"><span class="w">memoize</span></code>

jumps in and gives you the value out of the table, instead of letting
the function compute the value all over again.</p>
<p>Here is an extreme example.  Consider the Fibonacci sequence, defined
by the following function:</p>
<pre class="verbatim"><ol><li>	<span class="c"># Compute Fibonacci numbers</span></li><li><a name="fib"></a>	sub <span class="m">fib</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">$n</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	  <a class="l_k" href="functions/return.html">return</a> <span class="i">$n</span> if <span class="i">$n</span> &lt; <span class="n">2</span><span class="sc">;</span></li><li>	  <span class="i">fib</span><span class="s">(</span><span class="i">$n</span>-<span class="n">1</span><span class="s">)</span> + <span class="i">fib</span><span class="s">(</span><span class="i">$n</span>-<span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>This function is very slow.  Why?  To compute fib(14), it first wants
to compute fib(13) and fib(12), and add the results.  But to compute
fib(13), it first has to compute fib(12) and fib(11), and then it
comes back and computes fib(12) all over again even though the answer
is the same.  And both of the times that it wants to compute fib(12),
it has to compute fib(11) from scratch, and then it has to do it
again each time it wants to compute fib(13).  This function does so
much recomputing of old results that it takes a really long time to
run---fib(14) makes 1,200 extra recursive calls to itself, to compute
and recompute things that it already computed.</p>
<p>This function is a good candidate for memoization.  If you memoize the
`fib' function above, it will compute fib(14) exactly once, the first
time it needs to, and then save the result in a table.  Then if you
ask for fib(14) again, it gives you the result out of the table.
While computing fib(14), instead of computing fib(12) twice, it does
it once; the second time it needs the value it gets it from the table.
It doesn't compute fib(11) four times; it computes it once, getting it
from the table the next three times.  Instead of making 1,200
recursive calls to `fib', it makes 15.  This makes the function about
150 times faster.</p>
<p>You could do the memoization yourself, by rewriting the function, like
this:</p>
<pre class="verbatim"><ol><li>	<span class="c"># Compute Fibonacci numbers, memoized version</span></li><li>	<span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">@fib</span><span class="sc">;</span></li><li><a name="fib"></a>  	  sub <span class="m">fib</span> <span class="s">{</span></li><li>	    <a class="l_k" href="functions/my.html">my</a> <span class="i">$n</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	    <a class="l_k" href="functions/return.html">return</a> <span class="i">$fib</span>[<span class="i">$n</span>] if <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$fib</span>[<span class="i">$n</span>]<span class="sc">;</span></li><li>	    <a class="l_k" href="functions/return.html">return</a> <span class="i">$fib</span>[<span class="i">$n</span>] = <span class="i">$n</span> if <span class="i">$n</span> &lt; <span class="n">2</span><span class="sc">;</span></li><li>	    <span class="i">$fib</span>[<span class="i">$n</span>] = <span class="i">fib</span><span class="s">(</span><span class="i">$n</span>-<span class="n">1</span><span class="s">)</span> + <span class="i">fib</span><span class="s">(</span><span class="i">$n</span>-<span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li>	  <span class="s">}</span></li><li>        <span class="s">}</span></li></ol></pre><p>Or you could use this module, like this:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">Memoize</span><span class="sc">;</span></li><li>	<span class="i">memoize</span><span class="s">(</span><span class="q">&#39;fib&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="c"># Rest of the fib function just like the original version.</span></li></ol></pre><p>This makes it easy to turn memoizing on and off.</p>
<p>Here's an even simpler example: I wrote a simple ray tracer; the
program would look in a certain direction, figure out what it was
looking at, and then convert the `color' value (typically a string
like `red') of that object to a red, green, and blue pixel value, like
this:</p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span><span class="i">$direction</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$direction</span> &lt; <span class="n">300</span><span class="sc">;</span> <span class="i">$direction</span>++<span class="s">)</span> <span class="s">{</span></li><li>      <span class="c"># Figure out which object is in direction $direction</span></li><li>      <span class="i">$color</span> = <span class="i">$object</span>-&gt;{<span class="w">color</span>}<span class="sc">;</span></li><li>      <span class="s">(</span><span class="i">$r</span><span class="cm">,</span> <span class="i">$g</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@</span>{<span class="i">&amp;ColorToRGB</span><span class="s">(</span><span class="i">$color</span><span class="s">)</span>}<span class="sc">;</span></li><li>      ...</li><li>    <span class="s">}</span></li></ol></pre><p>Since there are relatively few objects in a picture, there are only a
few colors, which get looked up over and over again.  Memoizing
<code class="inline"><span class="w">ColorToRGB</span></code>
 sped up the program by several percent.</p>
<a name="DETAILS"></a><h1>DETAILS</h1>
<p>This module exports exactly one function, <code class="inline"><span class="w">memoize</span></code>
.  The rest of the
functions in this package are None of Your Business.</p>
<p>You should say</p>
<pre class="verbatim"><ol><li>	<span class="i">memoize</span><span class="s">(</span><span class="w">function</span><span class="s">)</span></li></ol></pre><p>where <code class="inline"><span class="w">function</span></code>
 is the name of the function you want to memoize, or
a reference to it.  <code class="inline"><span class="w">memoize</span></code>
 returns a reference to the new,
memoized version of the function, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> on a non-fatal error.
At present, there are no non-fatal errors, but there might be some in
the future.</p>
<p>If <code class="inline"><span class="w">function</span></code>
 was the name of a function, then <code class="inline"><span class="w">memoize</span></code>
 hides the
old version and installs the new memoized version under the old name,
so that <code class="inline"><span class="i">&amp;function</span><span class="s">(</span>...<span class="s">)</span></code>
 actually invokes the memoized version.</p>
<a name="OPTIONS"></a><h1>OPTIONS</h1>
<p>There are some optional options you can pass to <code class="inline"><span class="w">memoize</span></code>
 to change
the way it behaves a little.  To supply options, invoke <code class="inline"><span class="w">memoize</span></code>

like this:</p>
<pre class="verbatim"><ol><li>	<span class="i">memoize</span><span class="s">(</span><span class="w">function</span><span class="cm">,</span> <span class="w">NORMALIZER</span> <span class="cm">=&gt;</span> <span class="w">function</span><span class="cm">,</span></li><li>			  <span class="w">INSTALL</span> <span class="cm">=&gt;</span> <span class="w">newname</span><span class="cm">,</span></li><li>                          <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="w">option</span><span class="cm">,</span></li><li>	                  <span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="w">option</span></li><li>			 <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Each of these options is optional; you can include some, all, or none
of them.</p>
<a name="INSTALL"></a><h2>INSTALL</h2>
<p>If you supply a function name with <code class="inline"><span class="w">INSTALL</span></code>
, memoize will install
the new, memoized version of the function under the name you give.
For example,</p>
<pre class="verbatim"><ol><li>	<span class="i">memoize</span><span class="s">(</span><span class="q">&#39;fib&#39;</span><span class="cm">,</span> <span class="w">INSTALL</span> <span class="cm">=&gt;</span> <span class="q">&#39;fastfib&#39;</span><span class="s">)</span></li></ol></pre><p>installs the memoized version of <code class="inline"><span class="w">fib</span></code>
 as <code class="inline"><span class="w">fastfib</span></code>
; without the
<code class="inline"><span class="w">INSTALL</span></code>
 option it would have replaced the old <code class="inline"><span class="w">fib</span></code>
 with the
memoized version.</p>
<p>To prevent <code class="inline"><span class="w">memoize</span></code>
 from installing the memoized version anywhere, use
<code class="inline"><span class="w">INSTALL</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/undef.html">undef</a></code>
.</p>
<a name="NORMALIZER"></a><h2>NORMALIZER</h2>
<p>Suppose your function looks like this:</p>
<pre class="verbatim"><ol><li>	<span class="c"># Typical call: f(&#39;aha!&#39;, A =&gt; 11, B =&gt; 12);</span></li><li><a name="f"></a>	sub <span class="m">f</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	  <span class="i">$hash</span>{<span class="w">B</span>} ||= <span class="n">2</span><span class="sc">;</span>  <span class="c"># B defaults to 2</span></li><li>	  <span class="i">$hash</span>{<span class="w">C</span>} ||= <span class="n">7</span><span class="sc">;</span>  <span class="c"># C defaults to 7</span></li><li></li><li>	  <span class="c"># Do something with $a, %hash</span></li><li>	<span class="s">}</span></li></ol></pre><p>Now, the following calls to your function are all completely equivalent:</p>
<pre class="verbatim"><ol><li>	<span class="i">f</span><span class="s">(</span><span class="w">OUCH</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">f</span><span class="s">(</span><span class="w">OUCH</span><span class="cm">,</span> <span class="w">B</span> <span class="cm">=&gt;</span> <span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">f</span><span class="s">(</span><span class="w">OUCH</span><span class="cm">,</span> <span class="w">C</span> <span class="cm">=&gt;</span> <span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">f</span><span class="s">(</span><span class="w">OUCH</span><span class="cm">,</span> <span class="w">B</span> <span class="cm">=&gt;</span> <span class="n">2</span><span class="cm">,</span> <span class="w">C</span> <span class="cm">=&gt;</span> <span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">f</span><span class="s">(</span><span class="w">OUCH</span><span class="cm">,</span> <span class="w">C</span> <span class="cm">=&gt;</span> <span class="n">7</span><span class="cm">,</span> <span class="w">B</span> <span class="cm">=&gt;</span> <span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">(</span><span class="w">etc</span>.<span class="s">)</span></li></ol></pre><p>However, unless you tell <code class="inline"><span class="w">Memoize</span></code>
 that these calls are equivalent,
it will not know that, and it will compute the values for these
invocations of your function separately, and store them separately.</p>
<p>To prevent this, supply a <code class="inline"><span class="w">NORMALIZER</span></code>
 function that turns the
program arguments into a string in a way that equivalent arguments
turn into the same string.  A <code class="inline"><span class="w">NORMALIZER</span></code>
 function for <code class="inline"><span class="w">f</span></code>
 above
might look like this:</p>
<pre class="verbatim"><ol><li><a name="normalize_f"></a>	sub <span class="m">normalize_f</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">%hash</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	  <span class="i">$hash</span>{<span class="w">B</span>} ||= <span class="n">2</span><span class="sc">;</span></li><li>	  <span class="i">$hash</span>{<span class="w">C</span>} ||= <span class="n">7</span><span class="sc">;</span></li><li></li><li>	  <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">&#39;,&#39;</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <a class="l_k" href="functions/map.html">map</a> <span class="s">(</span><span class="i">$_</span> <span class="cm">=&gt;</span> <span class="i">$hash</span>{<span class="i">$_</span>}<span class="s">)</span> <a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>Each of the argument lists above comes out of the <code class="inline"><span class="w">normalize_f</span></code>

function looking exactly the same, like this:</p>
<pre class="verbatim"><ol><li>	<span class="w">OUCH</span><span class="cm">,</span><span class="w">B</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="w">C</span><span class="cm">,</span><span class="n">7</span></li></ol></pre><p>You would tell <code class="inline"><span class="w">Memoize</span></code>
 to use this normalizer this way:</p>
<pre class="verbatim"><ol><li>	<span class="i">memoize</span><span class="s">(</span><span class="q">&#39;f&#39;</span><span class="cm">,</span> <span class="w">NORMALIZER</span> <span class="cm">=&gt;</span> <span class="q">&#39;normalize_f&#39;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">memoize</span></code>
 knows that if the normalized version of the arguments is
the same for two argument lists, then it can safely look up the value
that it computed for one argument list and return it as the result of
calling the function with the other argument list, even if the
argument lists look different.</p>
<p>The default normalizer just concatenates the arguments with character
28 in between.  (In ASCII, this is called FS or control-\.)  This
always works correctly for functions with only one string argument,
and also when the arguments never contain character 28.  However, it
can confuse certain argument lists:</p>
<pre class="verbatim"><ol><li>	normalizer("a\034", "b")</li><li>	normalizer("a", "\034b")</li><li>	normalizer("a\034\034b")</li></ol></pre><p>for example.</p>
<p>Since hash keys are strings, the default normalizer will not
distinguish between <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> and the empty string.  It also won't work
when the function's arguments are references.  For example, consider a
function <code class="inline"><span class="w">g</span></code>
 which gets two arguments: A number, and a reference to
an array of numbers:</p>
<pre class="verbatim"><ol><li>	<span class="i">g</span><span class="s">(</span><span class="n">13</span><span class="cm">,</span> <span class="s">[</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="n">3</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">5</span><span class="cm">,</span><span class="n">6</span><span class="cm">,</span><span class="n">7</span><span class="s">]</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The default normalizer will turn this into something like
<code class="inline"><span class="q">&quot;13\034ARRAY(0x436c1f)&quot;</span></code>
.  That would be all right, except that a
subsequent array of numbers might be stored at a different location
even though it contains the same data.  If this happens, <code class="inline"><span class="w">Memoize</span></code>

will think that the arguments are different, even though they are
equivalent.  In this case, a normalizer like this is appropriate:</p>
<pre class="verbatim"><ol><li><a name="normalize"></a>	sub <span class="m">normalize</span> <span class="s">{</span> <a class="l_k" href="functions/join.html">join</a> <span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> <span class="i">@</span>{<span class="i">$_</span>[<span class="n">1</span>]} <span class="s">}</span></li></ol></pre><p>For the example above, this produces the key "13 1 2 3 4 5 6 7".</p>
<p>Another use for normalizers is when the function depends on data other
than those in its arguments.  Suppose you have a function which
returns a value which depends on the current hour of the day:</p>
<pre class="verbatim"><ol><li>	sub on_duty {</li><li>          my ($problem_type) = @_;</li><li>	  my $hour = (localtime)[2];</li><li>          open my $fh, "$DIR/$problem_type" or die...;</li><li>          my $line;</li><li>          while ($hour-- &gt; 0)</li><li>            $line = &lt;$fh&gt;;</li><li>          } </li><li>	  return $line;</li><li>	}</li></ol></pre><p>At 10:23, this function generates the 10th line of a data file; at
3:45 PM it generates the 15th line instead.  By default, <code class="inline"><span class="w">Memoize</span></code>

will only see the $problem_type argument.  To fix this, include the
current hour in the normalizer:</p>
<pre class="verbatim"><ol><li><a name="normalize"></a>        sub <span class="m">normalize</span> <span class="s">{</span> <a class="l_k" href="functions/join.html">join</a> <span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="s">(</span><a class="l_k" href="functions/localtime.html">localtime</a><span class="s">)</span>[<span class="n">2</span>]<span class="cm">,</span> <span class="i">@_</span> <span class="s">}</span></li></ol></pre><p>The calling context of the function (scalar or list context) is
propagated to the normalizer.  This means that if the memoized
function will treat its arguments differently in list context than it
would in scalar context, you can have the normalizer function select
its behavior based on the results of <code class="inline"><a class="l_k" href="functions/wantarray.html">wantarray</a></code>.  Even if called in
a list context, a normalizer should still return a single string.</p>
<a name="SCALAR_CACHE%2c-LIST_CACHE"></a><h2><code class="inline"><span class="w">SCALAR_CACHE</span></code>
, <code class="inline"><span class="w">LIST_CACHE</span></code>
</h2>
<p>Normally, <code class="inline"><span class="w">Memoize</span></code>
 caches your function's return values into an
ordinary Perl hash variable.  However, you might like to have the
values cached on the disk, so that they persist from one run of your
program to the next, or you might like to associate some other
interesting semantics with the cached values.</p>
<p>There's a slight complication under the hood of <code class="inline"><span class="w">Memoize</span></code>
: There are
actually <i>two</i> caches, one for scalar values and one for list values.
When your function is called in scalar context, its return value is
cached in one hash, and when your function is called in list context,
its value is cached in the other hash.  You can control the caching
behavior of both contexts independently with these options.</p>
<p>The argument to <code class="inline"><span class="w">LIST_CACHE</span></code>
 or <code class="inline"><span class="w">SCALAR_CACHE</span></code>
 must either be one of
the following four strings:</p>
<pre class="verbatim"><ol><li>	<span class="w">MEMORY</span></li><li>	<span class="w">FAULT</span></li><li>	<span class="w">MERGE</span></li><li>        <span class="w">HASH</span></li></ol></pre><p>or else it must be a reference to an array whose first element is one of
these four strings, such as <code class="inline"><span class="s">[</span><span class="w">HASH</span><span class="cm">,</span> <span class="w">arguments</span>...<span class="s">]</span></code>
.</p>
<ul>
<li><a name="MEMORY"></a><b><code class="inline"><span class="w">MEMORY</span></code>
</b>
<p><code class="inline"><span class="w">MEMORY</span></code>
 means that return values from the function will be cached in
an ordinary Perl hash variable.  The hash variable will not persist
after the program exits.  This is the default.</p>
</li>
<li><a name="HASH"></a><b><code class="inline"><span class="w">HASH</span></code>
</b>
<p><code class="inline"><span class="w">HASH</span></code>
 allows you to specify that a particular hash that you supply
will be used as the cache.  You can tie this hash beforehand to give
it any behavior you want.</p>
<p>A tied hash can have any semantics at all.  It is typically tied to an
on-disk database, so that cached values are stored in the database and
retrieved from it again when needed, and the disk file typically
persists after your program has exited.  See <code class="inline"><span class="w">perltie</span></code>
 for more
complete details about <code class="inline"><a class="l_k" href="functions/tie.html">tie</a></code>.</p>
<p>A typical example is:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/use.html">use</a> <span class="w">DB_File</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;DB_File&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span><span class="cm">,</span> <span class="n">0666</span><span class="sc">;</span></li><li>        <span class="w">memoize</span> <span class="q">&#39;function&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li></ol></pre><p>This has the effect of storing the cache in a <code class="inline"><span class="w">DB_File</span></code>
 database
whose name is in <code class="inline"><span class="i">$filename</span></code>
.  The cache will persist after the
program has exited.  Next time the program runs, it will find the
cache already populated from the previous run of the program.  Or you
can forcibly populate the cache by constructing a batch program that
runs in the background and populates the cache file.  Then when you
come to run your real program the memoized function will be fast
because all its results have been precomputed.</p>
<p>Another reason to use <code class="inline"><span class="w">HASH</span></code>
 is to provide your own hash variable.
You can then inspect or modify the contents of the hash to gain finer
control over the cache management.</p>
</li>
<li><a name="TIE"></a><b><code class="inline"><span class="w">TIE</span></code>
</b>
<p>This option is no longer supported.  It is still documented only to
aid in the debugging of old programs that use it.  Old programs should
be converted to use the <code class="inline"><span class="w">HASH</span></code>
 option instead.</p>
<pre class="verbatim"><ol><li>        <span class="w">memoize</span> ... <span class="s">[</span><span class="q">&#39;TIE&#39;</span><span class="cm">,</span> <span class="w">PACKAGE</span><span class="cm">,</span> <span class="w">ARGS</span>...<span class="s">]</span></li></ol></pre><p>is merely a shortcut for</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/require.html">require</a> <span class="w">PACKAGE</span><span class="sc">;</span></li><li>	<span class="s">{</span> <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span><span class="cm">,</span> <span class="w">PACKAGE</span><span class="cm">,</span> <span class="w">ARGS</span>...<span class="sc">;</span></li><li>          <span class="w">memoize</span> ... <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li><li>        <span class="s">}</span></li></ol></pre></li>
<li><a name="FAULT"></a><b><code class="inline"><span class="w">FAULT</span></code>
</b>
<p><code class="inline"><span class="w">FAULT</span></code>
 means that you never expect to call the function in scalar
(or list) context, and that if <code class="inline"><span class="w">Memoize</span></code>
 detects such a call, it
should abort the program.  The error message is one of</p>
<pre class="verbatim"><ol><li>	`foo' function called in forbidden list context at line ...</li><li>	`foo' function called in forbidden scalar context at line ...</li></ol></pre></li>
<li><a name="MERGE"></a><b><code class="inline"><span class="w">MERGE</span></code>
</b>
<p><code class="inline"><span class="w">MERGE</span></code>
 normally means that the memoized function does not
distinguish between list and sclar context, and that return values in
both contexts should be stored together.  Both <code class="inline"><span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span>
<span class="w">MERGE</span></code>
 and <code class="inline"><span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="w">MERGE</span></code>
 mean the same thing.</p>
<p>Consider this function:</p>
<pre class="verbatim"><ol><li><a name="complicated"></a>	sub <span class="m">complicated</span> <span class="s">{</span></li><li>          <span class="c"># ... time-consuming calculation of $result</span></li><li>          <a class="l_k" href="functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li>        <span class="s">}</span></li></ol></pre><p>The <code class="inline"><span class="w">complicated</span></code>
 function will return the same numeric <code class="inline"><span class="i">$result</span></code>

regardless of whether it is called in list or in scalar context.</p>
<p>Normally, the following code will result in two calls to <code class="inline"><span class="w">complicated</span></code>
, even
if <code class="inline"><span class="w">complicated</span></code>
 is memoized:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="i">complicated</span><span class="s">(</span><span class="n">142</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$y</span><span class="s">)</span> = <span class="i">complicated</span><span class="s">(</span><span class="n">142</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$z</span> = <span class="i">complicated</span><span class="s">(</span><span class="n">142</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The first call will cache the result, say 37, in the scalar cache; the
second will cach the list <code class="inline"><span class="s">(</span><span class="n">37</span><span class="s">)</span></code>
 in the list cache.  The third call
doesn't call the real <code class="inline"><span class="w">complicated</span></code>
 function; it gets the value 37
from the scalar cache.</p>
<p>Obviously, the second call to <code class="inline"><span class="w">complicated</span></code>
 is a waste of time, and
storing its return value is a waste of space.  Specifying <code class="inline"><span class="w">LIST_CACHE</span>
<span class="cm">=&gt;</span> <span class="w">MERGE</span></code>
 will make <code class="inline"><span class="w">memoize</span></code>
 use the same cache for scalar and
list context return values, so that the second call uses the scalar
cache that was populated by the first call.  <code class="inline"><span class="w">complicated</span></code>
 ends up
being called only once, and both subsequent calls return <code class="inline"><span class="n">3</span></code>
 from the
cache, regardless of the calling context.</p>
</li>
</ul>
<a name="List-values-in-scalar-context"></a><h3>List values in scalar context</h3>
<p>Consider this function:</p>
<pre class="verbatim"><ol><li><a name="iota"></a>    sub <span class="m">iota</span> <span class="s">{</span> <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/reverse.html">reverse</a> <span class="s">(</span><span class="n">1</span>..<span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span> <span class="s">}</span></li></ol></pre><p>This function normally returns a list.  Suppose you memoize it and
merge the caches:</p>
<pre class="verbatim"><ol><li>    <span class="w">memoize</span> <span class="q">&#39;iota&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MERGE&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">@i7</span> = <span class="i">iota</span><span class="s">(</span><span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$i7</span> = <span class="i">iota</span><span class="s">(</span><span class="n">7</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Here the first call caches the list (1,2,3,4,5,6,7).  The second call
does not really make sense. <code class="inline"><span class="w">Memoize</span></code>
 cannot guess what behavior
<code class="inline"><span class="w">iota</span></code>
 should have in scalar context without actually calling it in
scalar context.  Normally <code class="inline"><span class="w">Memoize</span></code>
 <i>would</i> call <code class="inline"><span class="w">iota</span></code>
 in scalar
context and cache the result, but the <code class="inline"><span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MERGE&#39;</span></code>

option says not to do that, but to use the cache list-context value
instead. But it cannot return a list of seven elements in a scalar
context. In this case <code class="inline"><span class="i">$i7</span></code>
 will receive the <b>first element</b> of the
cached list value, namely 7.</p>
<a name="Merged-disk-caches"></a><h3>Merged disk caches</h3>
<p>Another use for <code class="inline"><span class="w">MERGE</span></code>
 is when you want both kinds of return values
stored in the same disk file; this saves you from having to deal with
two disk files instead of one.  You can use a normalizer function to
keep the two sets of return values separate.  For example:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;MLDBM&#39;</span><span class="cm">,</span> <span class="q">&#39;DB_File&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> ...<span class="sc">;</span></li><li></li><li>	<span class="w">memoize</span> <span class="q">&#39;myfunc&#39;</span><span class="cm">,</span></li><li>	  <span class="w">NORMALIZER</span> <span class="cm">=&gt;</span> <span class="q">&#39;n&#39;</span><span class="cm">,</span></li><li>	  <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="cm">,</span></li><li>	  <span class="w">LIST_CACHE</span> <span class="cm">=&gt;</span> <span class="q">&#39;MERGE&#39;</span><span class="cm">,</span></li><li>	<span class="sc">;</span></li><li></li><li><a name="n"></a>	sub <span class="m">n</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">$context</span> = <a class="l_k" href="functions/wantarray.html">wantarray</a><span class="s">(</span><span class="s">)</span> ? <span class="q">&#39;L&#39;</span> <span class="co">:</span> <span class="q">&#39;S&#39;</span><span class="sc">;</span></li><li>	  <span class="c"># ... now compute the hash key from the arguments ...</span></li><li>	  <span class="i">$hashkey</span> = <span class="q">&quot;$context:$hashkey&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>This normalizer function will store scalar context return values in
the disk file under keys that begin with <code class="inline"><span class="j">S:</span></code>
, and list context
return values under keys that begin with <code class="inline"><span class="j">L:</span></code>
.</p>
<a name="OTHER-FACILITIES"></a><h1>OTHER FACILITIES</h1>
<a name="unmemoize"></a><h2><code class="inline"><span class="w">unmemoize</span></code>
</h2>
<p>There's an <code class="inline"><span class="w">unmemoize</span></code>
 function that you can import if you want to.
Why would you want to?  Here's an example: Suppose you have your cache
tied to a DBM file, and you want to make sure that the cache is
written out to disk if someone interrupts the program.  If the program
exits normally, this will happen anyway, but if someone types
control-C or something then the program will terminate immediately
without synchronizing the database.  So what you can do instead is</p>
<pre class="verbatim"><ol><li>    <span class="i">$SIG</span>{<span class="w">INT</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="w">unmemoize</span> <span class="q">&#39;function&#39;</span> <span class="s">}</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">unmemoize</span></code>
 accepts a reference to, or the name of a previously
memoized function, and undoes whatever it did to provide the memoized
version in the first place, including making the name refer to the
unmemoized version if appropriate.  It returns a reference to the
unmemoized version of the function.</p>
<p>If you ask it to unmemoize a function that was never memoized, it
croaks.</p>
<a name="flush_cache"></a><h2><code class="inline"><span class="w">flush_cache</span></code>
</h2>
<p><code class="inline"><span class="i">flush_cache</span><span class="s">(</span><span class="w">function</span><span class="s">)</span></code>
 will flush out the caches, discarding <i>all</i>
the cached data.  The argument may be a function name or a reference
to a function.  For finer control over when data is discarded or
expired, see the documentation for <code class="inline"><span class="w">Memoize::Expire</span></code>
, included in
this package.</p>
<p>Note that if the cache is a tied hash, <code class="inline"><span class="w">flush_cache</span></code>
 will attempt to
invoke the <code class="inline"><span class="w">CLEAR</span></code>
 method on the hash.  If there is no <code class="inline"><span class="w">CLEAR</span></code>

method, this will cause a run-time error.</p>
<p>An alternative approach to cache flushing is to use the <code class="inline"><span class="w">HASH</span></code>
 option
(see above) to request that <code class="inline"><span class="w">Memoize</span></code>
 use a particular hash variable
as its cache.  Then you can examine or modify the hash at any time in
any way you desire.  You may flush the cache by using <code class="inline"><span class="i">%hash</span> = <span class="s">(</span><span class="s">)</span></code>
.</p>
<a name="CAVEATS"></a><h1>CAVEATS</h1>
<p>Memoization is not a cure-all:</p>
<ul>
<li>
<p>Do not memoize a function whose behavior depends on program
state other than its own arguments, such as global variables, the time
of day, or file input.  These functions will not produce correct
results when memoized.  For a particularly easy example:</p>
<pre class="verbatim"><ol><li><a name="f"></a>	sub <span class="m">f</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/time.html">time</a><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>This function takes no arguments, and as far as <code class="inline"><span class="w">Memoize</span></code>
 is
concerned, it always returns the same result.  <code class="inline"><span class="w">Memoize</span></code>
 is wrong, of
course, and the memoized version of this function will call <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> once
to get the current time, and it will return that same time
every time you call it after that.</p>
</li>
<li>
<p>Do not memoize a function with side effects.</p>
<pre class="verbatim"><ol><li><a name="f"></a>	sub <span class="m">f</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>          <a class="l_k" href="functions/my.html">my</a> <span class="i">$s</span> = <span class="i">$a</span> + <span class="i">$b</span><span class="sc">;</span></li><li>	  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$a + $b = $s.\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>This function accepts two arguments, adds them, and prints their sum.
Its return value is the numuber of characters it printed, but you
probably didn't care about that.  But <code class="inline"><span class="w">Memoize</span></code>
 doesn't understand
that.  If you memoize this function, you will get the result you
expect the first time you ask it to print the sum of 2 and 3, but
subsequent calls will return 1 (the return value of
<code class="inline"><a class="l_k" href="functions/print.html">print</a></code>) without actually printing anything.</p>
</li>
<li>
<p>Do not memoize a function that returns a data structure that is
modified by its caller.</p>
<p>Consider these functions:  <code class="inline"><span class="w">getusers</span></code>
 returns a list of users somehow,
and then <code class="inline"><span class="w">main</span></code>
 throws away the first user on the list and prints the
rest:</p>
<pre class="verbatim"><ol><li><a name="main"></a>	sub <span class="m">main</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">$userlist</span> = <span class="i">getusers</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>	  <a class="l_k" href="functions/shift.html">shift</a> <span class="i">@$userlist</span><span class="sc">;</span></li><li>	  foreach <span class="i">$u</span> <span class="s">(</span><span class="i">@$userlist</span><span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;User $u\n&quot;</span><span class="sc">;</span></li><li>	  <span class="s">}</span></li><li>	<span class="s">}</span></li><li></li><li><a name="getusers"></a>	sub <span class="m">getusers</span> <span class="s">{</span></li><li>	  <a class="l_k" href="functions/my.html">my</a> <span class="i">@users</span><span class="sc">;</span></li><li>	  <span class="c"># Do something to get a list of users;</span></li><li>	  \<span class="i">@users</span><span class="sc">;</span>  <span class="c"># Return reference to list.</span></li><li>	<span class="s">}</span></li></ol></pre><p>If you memoize <code class="inline"><span class="w">getusers</span></code>
 here, it will work right exactly once.  The
reference to the users list will be stored in the memo table.  <code class="inline"><span class="w">main</span></code>

will discard the first element from the referenced list.  The next
time you invoke <code class="inline"><span class="w">main</span></code>
, <code class="inline"><span class="w">Memoize</span></code>
 will not call <code class="inline"><span class="w">getusers</span></code>
; it will
just return the same reference to the same list it got last time.  But
this time the list has already had its head removed; <code class="inline"><span class="w">main</span></code>
 will
erroneously remove another element from it.  The list will get shorter
and shorter every time you call <code class="inline"><span class="w">main</span></code>
.</p>
<p>Similarly, this:</p>
<pre class="verbatim"><ol><li>	<span class="i">$u1</span> = <span class="i">getusers</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>    </li><li>	<span class="i">$u2</span> = <span class="i">getusers</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>    </li><li>	<a class="l_k" href="functions/pop.html">pop</a> <span class="i">@$u1</span><span class="sc">;</span></li></ol></pre><p>will modify $u2 as well as $u1, because both variables are references
to the same array.  Had <code class="inline"><span class="w">getusers</span></code>
 not been memoized, $u1 and $u2
would have referred to different arrays.</p>
</li>
<li>
<p>Do not memoize a very simple function.</p>
<p>Recently someone mentioned to me that the Memoize module made his
program run slower instead of faster.  It turned out that he was
memoizing the following function:</p>
<pre class="verbatim"><ol><li><a name="square"></a>    sub <span class="m">square</span> <span class="s">{</span></li><li>      <span class="i">$_</span>[<span class="n">0</span>] * <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>I pointed out that <code class="inline"><span class="w">Memoize</span></code>
 uses a hash, and that looking up a
number in the hash is necessarily going to take a lot longer than a
single multiplication.  There really is no way to speed up the
<code class="inline"><span class="w">square</span></code>
 function.</p>
<p>Memoization is not magical.</p>
</li>
</ul>
<a name="PERSISTENT-CACHE-SUPPORT"></a><h1>PERSISTENT CACHE SUPPORT</h1>
<p>You can tie the cache tables to any sort of tied hash that you want
to, as long as it supports <code class="inline"><span class="w">TIEHASH</span></code>
, <code class="inline"><span class="w">FETCH</span></code>
, <code class="inline"><span class="w">STORE</span></code>
, and
<code class="inline"><span class="w">EXISTS</span></code>
.  For example,</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;GDBM_File&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span><span class="cm">,</span> <span class="n">0666</span><span class="sc">;</span></li><li>        <span class="w">memoize</span> <span class="q">&#39;function&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li></ol></pre><p>works just fine.  For some storage methods, you need a little glue.</p>
<p><code class="inline"><span class="w">SDBM_File</span></code>
 doesn't supply an <code class="inline"><span class="w">EXISTS</span></code>
 method, so included in this
package is a glue module called <code class="inline"><span class="w">Memoize::SDBM_File</span></code>
 which does
provide one.  Use this instead of plain <code class="inline"><span class="w">SDBM_File</span></code>
 to store your
cache table on disk in an <code class="inline"><span class="w">SDBM_File</span></code>
 database:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;Memoize::SDBM_File&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span><span class="cm">,</span> <span class="n">0666</span><span class="sc">;</span></li><li>        <span class="w">memoize</span> <span class="q">&#39;function&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">NDBM_File</span></code>
 has the same problem and the same solution.  (Use
<code class="inline"><span class="w">Memoize::NDBM_File</span> <span class="w">instead</span> <span class="w">of</span> <span class="w">plain</span> <span class="w">NDBM_File</span>.</code>
)</p>
<p><code class="inline"><span class="w">Storable</span></code>
 isn't a tied hash class at all.  You can use it to store a
hash to disk and retrieve it again, but you can't modify the hash while
it's on the disk.  So if you want to store your cache table in a
<code class="inline"><span class="w">Storable</span></code>
 database, use <code class="inline"><span class="w">Memoize::Storable</span></code>
, which puts a hashlike
front-end onto <code class="inline"><span class="w">Storable</span></code>
.  The hash table is actually kept in
memory, and is loaded from your <code class="inline"><span class="w">Storable</span></code>
 file at the time you
memoize the function, and stored back at the time you unmemoize the
function (or when your program exits):</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;Memoize::Storable&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="sc">;</span></li><li>	<span class="w">memoize</span> <span class="q">&#39;function&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li><li></li><li>        <a class="l_k" href="functions/tie.html">tie</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">%cache</span> <span class="cm">=&gt;</span> <span class="q">&#39;Memoize::Storable&#39;</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="q">&#39;nstore&#39;</span><span class="sc">;</span></li><li>	<span class="w">memoize</span> <span class="q">&#39;function&#39;</span><span class="cm">,</span> <span class="w">SCALAR_CACHE</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="w">HASH</span> <span class="cm">=&gt;</span> \<span class="i">%cache</span><span class="s">]</span><span class="sc">;</span></li></ol></pre><p>Include the `nstore' option to have the <code class="inline"><span class="w">Storable</span></code>
 database written
in `network order'.  (See <a href="Storable.html">Storable</a> for more details about this.)</p>
<p>The <code class="inline"><span class="i">flush_cache</span><span class="s">(</span><span class="s">)</span></code>
 function will raise a run-time error unless the
tied package provides a <code class="inline"><span class="w">CLEAR</span></code>
 method.</p>
<a name="EXPIRATION-SUPPORT"></a><h1>EXPIRATION SUPPORT</h1>
<p>See Memoize::Expire, which is a plug-in module that adds expiration
functionality to Memoize.  If you don't like the kinds of policies
that Memoize::Expire implements, it is easy to write your own plug-in
module to implement whatever policy you desire.  Memoize comes with
several examples.  An expiration manager that implements a LRU policy
is available on CPAN as Memoize::ExpireLRU.</p>
<a name="BUGS"></a><h1>BUGS</h1>
<p>The test suite is much better, but always needs improvement.</p>
<p>There is some problem with the way <code class="inline"><a class="l_k" href="functions/goto.html">goto</a> <span class="i">&amp;f</span></code>
 works under threaded
Perl, perhaps because of the lexical scoping of <code class="inline"><span class="i">@_</span></code>
.  This is a bug
in Perl, and until it is resolved, memoized functions will see a
slightly different <code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code> and will perform a little more slowly
on threaded perls than unthreaded perls.</p>
<p>Some versions of <code class="inline"><span class="w">DB_File</span></code>
 won't let you store data under a key of
length 0.  That means that if you have a function <code class="inline"><span class="w">f</span></code>
 which you
memoized and the cache is in a <code class="inline"><span class="w">DB_File</span></code>
 database, then the value of
<code class="inline"><span class="i">f</span><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><span class="w">f</span></code>
 called with no arguments) will not be memoized.  If this
is a big problem, you can supply a normalizer function that prepends
<code class="inline"><span class="q">&quot;x&quot;</span></code>
 to every key.</p>
<a name="MAILING-LIST"></a><h1>MAILING LIST</h1>
<p>To join a very low-traffic mailing list for announcements about
<code class="inline"><span class="w">Memoize</span></code>
, send an empty note to <code class="inline"><span class="w">mjd</span>-<span class="w">perl</span>-<span class="w">memoize</span>-<span class="w">request</span><span class="i">@plover</span>.<span class="w">com</span></code>
.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Mark-Jason Dominus (<code class="inline"><span class="w">mjd</span>-<span class="w">perl</span>-<span class="w">memoize</span>+<span class="i">@plover</span>.<span class="w">com</span></code>
), Plover Systems co.</p>
<p>See the <code class="inline"><span class="w">Memoize</span>.<span class="w">pm</span></code>
 Page at <a href="http://perl.plover.com/Memoize/">http://perl.plover.com/Memoize/</a>
for news and upgrades.  Near this page, at
<a href="http://perl.plover.com/MiniMemoize/">http://perl.plover.com/MiniMemoize/</a> there is an article about
memoization and about the internals of Memoize that appeared in The
Perl Journal, issue #13.  (This article is also included in the
Memoize distribution as `article.html'.)</p>
<p>The author's book <i>Higher-Order Perl</i> (2005, ISBN 1558607013, published
by Morgan Kaufmann) discusses memoization (and many other 
topics) in tremendous detail. It is available on-line for free.
For more information, visit <a href="http://hop.perl.plover.com/">http://hop.perl.plover.com/</a> .</p>
<p>To join a mailing list for announcements about <code class="inline"><span class="w">Memoize</span></code>
, send an
empty message to <code class="inline"><span class="w">mjd</span>-<span class="w">perl</span>-<span class="w">memoize</span>-<span class="w">request</span><span class="i">@plover</span>.<span class="w">com</span></code>
.  This mailing
list is for announcements only and has extremely low traffic---fewer than
two messages per year.</p>
<a name="COPYRIGHT-AND-LICENSE"></a><h1>COPYRIGHT AND LICENSE</h1>
<p>Copyright 1998, 1999, 2000, 2001, 2012  by Mark Jason Dominus</p>
<p>This library is free software; you may redistribute it and/or modify
it under the same terms as Perl itself.</p>
<a name="THANK-YOU"></a><h1>THANK YOU</h1>
<p>Many thanks to Florian Ragwitz for administration and packaging
assistance, to John Tromp for bug reports, to Jonathan Roy for bug reports
and suggestions, to Michael Schwern for other bug reports and patches,
to Mike Cariaso for helping me to figure out the Right Thing to Do
About Expiration, to Joshua Gerth, Joshua Chamas, Jonathan Roy
(again), Mark D. Anderson, and Andrew Johnson for more suggestions
about expiration, to Brent Powers for the Memoize::ExpireLRU module,
to Ariel Scolnicov for delightful messages about the Fibonacci
function, to Dion Almaer for thought-provoking suggestions about the
default normalizer, to Walt Mankowski and Kurt Starsinic for much help
investigating problems under threaded Perl, to Alex Dudkevich for
reporting the bug in prototyped functions and for checking my patch,
to Tony Bass for many helpful suggestions, to Jonathan Roy (again) for
finding a use for <code class="inline"><span class="i">unmemoize</span><span class="s">(</span><span class="s">)</span></code>
, to Philippe Verdret for enlightening
discussion of <code class="inline"><span class="w">Hook::PrePostCall</span></code>
, to Nat Torkington for advice I
ignored, to Chris Nandor for portability advice, to Randal Schwartz
for suggesting the '<code class="inline"><span class="w">flush_cache</span></code>
 function, and to Jenda Krynicky for
being a light in the world.</p>
<p>Special thanks to Jarkko Hietaniemi, the 5.8.0 pumpking, for including
this module in the core and for his patient and helpful guidance
during the integration process.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#DETAILS">DETAILS</a><li><a href="#OPTIONS">OPTIONS</a><ul><li><a href="#INSTALL">INSTALL</a><li><a href="#NORMALIZER">NORMALIZER</a><li><a href="#SCALAR_CACHE%2c-LIST_CACHE">SCALAR_CACHE, LIST_CACHE</a></ul><li><a href="#OTHER-FACILITIES">OTHER FACILITIES</a><ul><li><a href="#unmemoize">unmemoize</a><li><a href="#flush_cache">flush_cache</a></ul><li><a href="#CAVEATS">CAVEATS</a><li><a href="#PERSISTENT-CACHE-SUPPORT">PERSISTENT CACHE SUPPORT</a><li><a href="#EXPIRATION-SUPPORT">EXPIRATION SUPPORT</a><li><a href="#BUGS">BUGS</a><li><a href="#MAILING-LIST">MAILING LIST</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#COPYRIGHT-AND-LICENSE">COPYRIGHT AND LICENSE</a><li><a href="#THANK-YOU">THANK YOU</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'Memoize';
  perldoc.pageAddress = 'Memoize.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>