File: overload.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 (1506 lines) | stat: -rw-r--r-- 146,050 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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>overload - 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>overload</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-pragmas.html">Pragmas</a> &gt;
      
    
    overload
  

            </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>overload</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Fundamentals">Fundamentals</a><li><a href="#Overloadable-Operations">Overloadable Operations</a><li><a href="#Magic-Autogeneration">Magic Autogeneration</a><li><a href="#Special-Keys-for-use-overload">Special Keys for use overload</a><li><a href="#How-Perl-Chooses-an-Operator-Implementation">How Perl Chooses an Operator Implementation</a><li><a href="#Losing-Overloading">Losing Overloading</a><li><a href="#Inheritance-and-Overloading">Inheritance and Overloading</a><li><a href="#Run-time-Overloading">Run-time Overloading</a><li><a href="#Public-Functions">Public Functions</a><li><a href="#Overloading-Constants">Overloading Constants</a></ul><li><a href="#IMPLEMENTATION">IMPLEMENTATION</a><li><a href="#COOKBOOK">COOKBOOK</a><ul><li><a href="#Two-face-Scalars">Two-face Scalars</a><li><a href="#Two-face-References">Two-face References</a><li><a href="#Symbolic-Calculator">Symbolic Calculator</a><li><a href="#_Really_-Symbolic-Calculator">_Really_ Symbolic Calculator</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#DIAGNOSTICS">DIAGNOSTICS</a><li><a href="#BUGS-AND-PITFALLS">BUGS AND PITFALLS</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>overload - Package for overloading Perl operations</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li><a name="package-SomeThing"></a>    package <span class="i">SomeThing</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></li><li>	<span class="q">&#39;+&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;myadd</span><span class="cm">,</span></li><li>	<span class="q">&#39;-&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;mysub</span><span class="sc">;</span></li><li>	<span class="c"># etc</span></li><li>    ...</li><li></li><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="w">SomeThing</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="n">57</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="n">5</span> + <span class="i">$a</span><span class="sc">;</span></li><li>    ...</li><li>    if <span class="s">(</span><span class="w">overload::Overloaded</span> <span class="i">$b</span><span class="s">)</span> <span class="s">{</span>...<span class="s">}</span></li><li>    ...</li><li>    <span class="i">$strval</span> = <span class="w">overload::StrVal</span> <span class="i">$b</span><span class="sc">;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This pragma allows overloading of Perl's operators for a class.
To overload built-in functions, see <a href="perlsub.html#Overriding-Built-in-Functions">Overriding Built-in Functions in perlsub</a> instead.</p>
<a name="Fundamentals"></a><h2>Fundamentals</h2>
<a name="Declaration"></a><h3>Declaration</h3>
<p>Arguments of the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive are (key, value) pairs.
For the full set of legal keys, see <a href="#Overloadable-Operations">Overloadable Operations</a> below.</p>
<p>Operator implementations (the values) can be subroutines,
references to subroutines, or anonymous subroutines
- in other words, anything legal inside a <code class="inline"><span class="i">&amp;</span>{ ... }</code>
 call.
Values specified as strings are interpreted as method names.
Thus</p>
<pre class="verbatim"><ol><li><a name="package-Number"></a>    package <span class="i">Number</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></li><li>        <span class="q">&quot;-&quot;</span> <span class="cm">=&gt;</span> <span class="q">&quot;minus&quot;</span><span class="cm">,</span></li><li>        <span class="q">&quot;*=&quot;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;muas</span><span class="cm">,</span></li><li>        <span class="q">&#39;&quot;&quot;&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> ...<span class="sc">;</span> <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>declares that subtraction is to be implemented by method <code class="inline"><span class="i">minus</span><span class="s">(</span><span class="s">)</span></code>

in the class <code class="inline"><span class="w">Number</span></code>
 (or one of its base classes),
and that the function <code class="inline"><span class="i">Number::muas</span><span class="s">(</span><span class="s">)</span></code>
 is to be used for the
assignment form of multiplication, <code class="inline"><span class="i">*=</span></code>
.
It also defines an anonymous subroutine to implement stringification:
this is called whenever an object blessed into the package <code class="inline"><span class="w">Number</span></code>

is used in a string context (this subroutine might, for example,
return the number as a Roman numeral).</p>
<a name="Calling-Conventions-and-Magic-Autogeneration"></a><h3>Calling Conventions and Magic Autogeneration</h3>
<p>The following sample implementation of <code class="inline"><span class="i">minus</span><span class="s">(</span><span class="s">)</span></code>
 (which assumes
that <code class="inline"><span class="w">Number</span></code>
 objects are simply blessed references to scalars)
illustrates the calling conventions:</p>
<pre class="verbatim"><ol><li><a name="package-Number"></a>    package <span class="i">Number</span><span class="sc">;</span></li><li><a name="minus"></a>    sub <span class="m">minus</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="i">$other</span><span class="cm">,</span> <span class="i">$swap</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">$result</span> = <span class="i">$$self</span> - <span class="i">$other</span><span class="sc">;</span>         <span class="c"># *</span></li><li>        <span class="i">$result</span> = -<span class="i">$result</span> if <span class="i">$swap</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$result</span> ? <span class="i">$result</span> <span class="co">:</span> <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$result</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="c"># * may recurse once - see table below</span></li></ol></pre><p>Three arguments are passed to all subroutines specified in the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive (with exceptions - see below, particularly
<a href="#nomethod">nomethod</a>).</p>
<p>The first of these is the operand providing the overloaded
operator implementation -
in this case, the object whose <code class="inline"><span class="i">minus</span><span class="s">(</span><span class="s">)</span></code>
 method is being called.</p>
<p>The second argument is the other operand, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> in the
case of a unary operator.</p>
<p>The third argument is set to TRUE if (and only if) the two
operands have been swapped.  Perl may do this to ensure that the
first argument (<code class="inline"><span class="i">$self</span></code>
) is an object implementing the overloaded
operation, in line with general object calling conventions.
For example, if <code class="inline"><span class="i">$x</span></code>
 and <code class="inline"><span class="i">$y</span></code>
 are <code class="inline"><span class="w">Number</span></code>
s:</p>
<pre class="verbatim"><ol><li>    <span class="w">operation</span>   |   <span class="w">generates</span> <span class="w">a</span> <span class="w">call</span> <span class="w">to</span></li><li>    ============|======================</li><li>    <span class="i">$x</span> - <span class="i">$y</span>     |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span> <span class="q">&#39;&#39;</span><span class="s">)</span></li><li>    <span class="i">$x</span> - <span class="n">7</span>      |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="q">&#39;&#39;</span><span class="s">)</span></li><li>    <span class="n">7</span> - <span class="i">$x</span>      |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span></li></ol></pre><p>Perl may also use <code class="inline"><span class="i">minus</span><span class="s">(</span><span class="s">)</span></code>
 to implement other operators which
have not been specified in the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive,
according to the rules for <a href="#Magic-Autogeneration">Magic Autogeneration</a> described later.
For example, the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 above declared no subroutine
for any of the operators <code class="inline">--</code>
, <code class="inline"><span class="w">neg</span></code>
 (the overload key for
unary minus), or <code class="inline">-=</code>
.  Thus</p>
<pre class="verbatim"><ol><li>    <span class="w">operation</span>   |   <span class="w">generates</span> <span class="w">a</span> <span class="w">call</span> <span class="w">to</span></li><li>    ============|======================</li><li>    -<span class="i">$x</span>         |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span></li><li>    <span class="i">$x</span>--        |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span></li><li>    <span class="i">$x</span> -= <span class="n">3</span>     |   <span class="i">minus</span><span class="s">(</span><span class="i">$x</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span></li></ol></pre><p>Note the <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>s:
where autogeneration results in the method for a standard
operator which does not change either of its operands, such
as <code class="inline">-</code>
, being used to implement an operator which changes
the operand ("mutators": here, <code class="inline">--</code>
 and <code class="inline">-=</code>
),
Perl passes undef as the third argument.
This still evaluates as FALSE, consistent with the fact that
the operands have not been swapped, but gives the subroutine
a chance to alter its behaviour in these cases.</p>
<p>In all the above examples, <code class="inline"><span class="i">minus</span><span class="s">(</span><span class="s">)</span></code>
 is required
only to return the result of the subtraction:
Perl takes care of the assignment to $x.
In fact, such methods should <i>not</i> modify their operands,
even if <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is passed as the third argument
(see <a href="#Overloadable-Operations">Overloadable Operations</a>).</p>
<p>The same is not true of implementations of <code class="inline">++</code>
 and <code class="inline">--</code>
:
these are expected to modify their operand.
An appropriate implementation of <code class="inline">--</code>
 might look like</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;--&#39;</span> <span class="cm">=&gt;</span> <span class="q">&quot;decr&quot;</span><span class="cm">,</span></li><li>        <span class="c"># ...</span></li><li><a name="decr"></a>    sub <span class="m">decr</span> <span class="s">{</span> --<span class="i">$</span>{<span class="i">$_</span>[<span class="n">0</span>]}<span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>If the experimental "bitwise" feature is enabled (see <a href="feature.html">feature</a>), a fifth
TRUE argument is passed to subroutines handling <code class="inline"><span class="i">&amp;</span></code>
, <code class="inline">|</code>, <code class="inline">^</code> and <code class="inline">~</code>
.
This indicates that the caller is expecting numeric behaviour.  The fourth
argument will be <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, as that position (<code class="inline"><span class="i">$_</span>[<span class="n">3</span>]</code>
) is reserved for use
by <a href="#nomethod">nomethod</a>.</p>
<a name="Mathemagic%2c-Mutators%2c-and-Copy-Constructors"></a><h3>Mathemagic, Mutators, and Copy Constructors</h3>
<p>The term 'mathemagic' describes the overloaded implementation
of mathematical operators.
Mathemagical operations raise an issue.
Consider the code:</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> = <span class="i">$b</span><span class="sc">;</span></li><li>    --<span class="i">$a</span><span class="sc">;</span></li></ol></pre><p>If <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 are scalars then after these statements</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> == <span class="i">$b</span> - <span class="n">1</span></li></ol></pre><p>An object, however, is a reference to blessed data, so if
<code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 are objects then the assignment <code class="inline"><span class="i">$a</span> = <span class="i">$b</span></code>

copies only the reference, leaving <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 referring
to the same object data.
One might therefore expect the operation <code class="inline">--<span class="i">$a</span></code>
 to decrement
<code class="inline"><span class="i">$b</span></code>
 as well as <code class="inline"><span class="i">$a</span></code>
.
However, this would not be consistent with how we expect the
mathematical operators to work.</p>
<p>Perl resolves this dilemma by transparently calling a copy
constructor before calling a method defined to implement
a mutator (<code class="inline">--</code>
, <code class="inline">+=</code>
, and so on.).
In the above example, when Perl reaches the decrement
statement, it makes a copy of the object data in <code class="inline"><span class="i">$a</span></code>
 and
assigns to <code class="inline"><span class="i">$a</span></code>
 a reference to the copied data.
Only then does it call <code class="inline"><span class="i">decr</span><span class="s">(</span><span class="s">)</span></code>
, which alters the copied
data, leaving <code class="inline"><span class="i">$b</span></code>
 unchanged.
Thus the object metaphor is preserved as far as possible,
while mathemagical operations still work according to the
arithmetic metaphor.</p>
<p>Note: the preceding paragraph describes what happens when
Perl autogenerates the copy constructor for an object based
on a scalar.
For other cases, see <a href="#Copy-Constructor">Copy Constructor</a>.</p>
<a name="Overloadable-Operations"></a><h2>Overloadable Operations</h2>
<p>The complete list of keys that can be specified in the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>

directive are given, separated by spaces, in the values of the
hash <code class="inline"><span class="i">%overload::ops</span></code>
:</p>
<pre class="verbatim"><ol><li> <span class="w">with_assign</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;+ - * / % ** &lt;&lt; &gt;&gt; x .&#39;</span><span class="cm">,</span></li><li> <span class="w">assign</span>		  <span class="cm">=&gt;</span> <span class="q">&#39;+= -= *= /= %= **= &lt;&lt;= &gt;&gt;= x= .=&#39;</span><span class="cm">,</span></li><li> <span class="w">num_comparison</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;&lt; &lt;= &gt; &gt;= == !=&#39;</span><span class="cm">,</span></li><li> <span class="q">&#39;3way_comparison&#39;</span><span class="cm">=&gt;</span> <span class="q">&#39;&lt;=&gt; cmp&#39;</span><span class="cm">,</span></li><li> <span class="w">str_comparison</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;lt le gt ge eq ne&#39;</span><span class="cm">,</span></li><li> <span class="w">binary</span>		  <span class="cm">=&gt;</span> <span class="q">&#39;&amp; &amp;= | |= ^ ^= &amp;. &amp;.= |. |.= ^. ^.=&#39;</span><span class="cm">,</span></li><li> <span class="w">unary</span>		  <span class="cm">=&gt;</span> <span class="q">&#39;neg ! ~ ~.&#39;</span><span class="cm">,</span></li><li> <span class="w">mutators</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;++ --&#39;</span><span class="cm">,</span></li><li> <span class="w">func</span>		  <span class="cm">=&gt;</span> <span class="q">&#39;atan2 cos sin exp abs log sqrt int&#39;</span><span class="cm">,</span></li><li> <span class="w">conversion</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;bool &quot;&quot; 0+ qr&#39;</span><span class="cm">,</span></li><li> <span class="w">iterators</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;&lt;&gt;&#39;</span><span class="cm">,</span></li><li> <span class="w">filetest</span>         <span class="cm">=&gt;</span> <span class="q">&#39;-X&#39;</span><span class="cm">,</span></li><li> <span class="w">dereferencing</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;${} @{} %{} &amp;{} *{}&#39;</span><span class="cm">,</span></li><li> <span class="w">matching</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;~~&#39;</span><span class="cm">,</span></li><li> <span class="w">special</span>	  <span class="cm">=&gt;</span> <span class="q">&#39;nomethod fallback =&#39;</span></li></ol></pre><p>Most of the overloadable operators map one-to-one to these keys.
Exceptions, including additional overloadable operations not
apparent from this hash, are included in the notes which follow.
This list is subject to growth over time.</p>
<p>A warning is issued if an attempt is made to register an operator not found
above.</p>
<ul>
<li><a name="*-not"></a><b><code class="inline">not</code>
</b>
<p>The operator <code class="inline">not</code>
 is not a valid key for <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
.
However, if the operator <code class="inline">!</code>
 is overloaded then the same
implementation will be used for <code class="inline">not</code>

(since the two operators differ only in precedence).</p>
</li>
<li><a name="*-neg"></a><b><code class="inline"><span class="w">neg</span></code>
</b>
<p>The key <code class="inline"><span class="w">neg</span></code>
 is used for unary minus to disambiguate it from
binary <code class="inline">-</code>
.</p>
</li>
<li><a name="*-%2b%2b%2c---"></a><b><code class="inline">++</code>
, <code class="inline">--</code>
</b>
<p>Assuming they are to behave analogously to Perl's <code class="inline">++</code>
 and <code class="inline">--</code>
,
overloaded implementations of these operators are required to
mutate their operands.</p>
<p>No distinction is made between prefix and postfix forms of the
increment and decrement operators: these differ only in the
point at which Perl calls the associated subroutine when
evaluating an expression.</p>
</li>
<li><a name="*-_Assignments_"></a><b><i>Assignments</i></b>
<pre class="verbatim"><ol><li>    +=  -=  <span class="i">*=</span>  /=  %=  <span class="i">**</span>=  &lt;&lt;=  &gt;&gt;=  <span class="w">x</span>=  .=</li><li>    &amp;=  |=  ^=  <span class="i">&amp;.</span>=  |.=  ^.=</li></ol></pre><p>Simple assignment is not overloadable (the <code class="inline"><span class="q">&#39;=&#39;</span></code>
 key is used
for the <a href="#Copy-Constructor">Copy Constructor</a>).
Perl does have a way to make assignments to an object do whatever
you want, but this involves using tie(), not overload -
see <a href="functions/tie.html">tie</a> and the <a href="#COOKBOOK">COOKBOOK</a> examples below.</p>
<p>The subroutine for the assignment variant of an operator is
required only to return the result of the operation.
It is permitted to change the value of its operand
(this is safe because Perl calls the copy constructor first),
but this is optional since Perl assigns the returned value to
the left-hand operand anyway.</p>
<p>An object that overloads an assignment operator does so only in
respect of assignments to that object.
In other words, Perl never calls the corresponding methods with
the third argument (the "swap" argument) set to TRUE.
For example, the operation</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> *= <span class="i">$b</span></li></ol></pre><p>cannot lead to <code class="inline"><span class="i">$b</span></code>
's implementation of <code class="inline"><span class="i">*=</span></code>
 being called,
even if <code class="inline"><span class="i">$a</span></code>
 is a scalar.
(It can, however, generate a call to <code class="inline"><span class="i">$b</span></code>
's method for <code class="inline"><span class="i">*</span></code>
).</p>
</li>
<li><a name="*-_Non-mutators-with-a-mutator-variant_"></a><b><i>Non-mutators with a mutator variant</i></b>
<pre class="verbatim"><ol><li>     +  -  <span class="i">*  /</span>  %  <span class="i">**</span>  &lt;&lt;  &gt;&gt;  <span class="w">x</span>  .</li><li>     <span class="i">&amp;  |</span>  ^  <span class="i">&amp;.</span>  |.  ^.</li></ol></pre><p>As described <a href="#Calling-Conventions-and-Magic-Autogeneration">above</a>,
Perl may call methods for operators like <code class="inline">+</code>
 and <code class="inline"><span class="i">&amp;</span></code>
 in the course
of implementing missing operations like <code class="inline">++</code>
, <code class="inline">+=</code>
, and <code class="inline">&amp;=</code>
.
While these methods may detect this usage by testing the definedness
of the third argument, they should in all cases avoid changing their
operands.
This is because Perl does not call the copy constructor before
invoking these methods.</p>
</li>
<li><a name="*-int"></a><b><code class="inline"><a class="l_k" href="functions/int.html">int</a></code></b>
<p>Traditionally, the Perl function <code class="inline"><a class="l_k" href="functions/int.html">int</a></code> rounds to 0
(see <a href="functions/int.html">int</a>), and so for floating-point-like types one
should follow the same semantic.</p>
</li>
<li><a name="*-_String%2c-numeric%2c-boolean%2c-and-regexp-conversions_"></a><b><i>String, numeric, boolean, and regexp conversions</i></b>
<pre class="verbatim"><ol><li>    <span class="q">&quot;&quot;</span>  <span class="n">0</span>+  <span class="w">bool</span></li></ol></pre><p>These conversions are invoked according to context as necessary.
For example, the subroutine for <code class="inline"><span class="q">&#39;&quot;&quot;&#39;</span></code>
 (stringify) may be used
where the overloaded object is passed as an argument to <code class="inline"><a class="l_k" href="functions/print.html">print</a></code>,
and that for <code class="inline"><span class="q">&#39;bool&#39;</span></code>
 where it is tested in the condition of a flow
control statement (like <code class="inline">while</code>
) or the ternary <code class="inline">?:</code> operation.</p>
<p>Of course, in contexts like, for example, <code class="inline"><span class="i">$obj</span> + <span class="n">1</span></code>
, Perl will
invoke <code class="inline"><span class="i">$obj</span></code>
's implementation of <code class="inline">+</code>
 rather than (in this
example) converting <code class="inline"><span class="i">$obj</span></code>
 to a number using the numify method
<code class="inline"><span class="q">&#39;0+&#39;</span></code>
 (an exception to this is when no method has been provided
for <code class="inline"><span class="q">&#39;+&#39;</span></code>
 and <a href="#fallback">fallback</a> is set to TRUE).</p>
<p>The subroutines for <code class="inline"><span class="q">&#39;&quot;&quot;&#39;</span></code>
, <code class="inline"><span class="q">&#39;0+&#39;</span></code>
, and <code class="inline"><span class="q">&#39;bool&#39;</span></code>
 can return
any arbitrary Perl value.
If the corresponding operation for this value is overloaded too,
the operation will be called again with this value.</p>
<p>As a special case if the overload returns the object itself then it will
be used directly.  An overloaded conversion returning the object is
probably a bug, because you're likely to get something that looks like
<code class="inline"><span class="w">YourPackage</span>=<span class="i">HASH</span><span class="s">(</span><span class="n">0x8172b34</span><span class="s">)</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="q">qr</span></li></ol></pre><p>The subroutine for <code class="inline"><span class="q">&#39;qr&#39;</span></code>
 is used wherever the object is
interpolated into or used as a regexp, including when it
appears on the RHS of a <code class="inline">=~</code>
 or <code class="inline">!~</code>
 operator.</p>
<p><code class="inline"><a class="l_k" href="functions/qr.html">qr</a></code> must return a compiled regexp, or a ref to a compiled regexp
(such as <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code> returns), and any further overloading on the return
value will be ignored.</p>
</li>
<li><a name="*-_Iteration_"></a><b><i>Iteration</i></b>
<p>If <code class="inline">&lt;&gt;</code>
 is overloaded then the same implementation is used
for both the <i>read-filehandle</i> syntax <code class="inline"><span class="q">&lt;$var&gt;</span></code>
 and
<i>globbing</i> syntax <code class="inline"><span class="q">&lt;${var}&gt;</span></code>
.</p>
</li>
<li><a name="*-_File-tests_"></a><b><i>File tests</i></b>
<p>The key <code class="inline"><span class="q">&#39;-X&#39;</span></code>
 is used to specify a subroutine to handle all the
filetest operators (<code class="inline">-f</code>
, <code class="inline">-x</code>
, and so on: see <a href="functions/-X.html">-X</a> for
the full list);
it is not possible to overload any filetest operator individually.
To distinguish them, the letter following the '-' is passed as the
second argument (that is, in the slot that for binary operators
is used to pass the second operand).</p>
<p>Calling an overloaded filetest operator does not affect the stat value
associated with the special filehandle <code class="inline"><span class="w">_</span></code>
.  It still refers to the
result of the last <code class="inline"><a class="l_k" href="functions/stat.html">stat</a></code>, <code class="inline"><a class="l_k" href="functions/lstat.html">lstat</a></code> or unoverloaded filetest.</p>
<p>This overload was introduced in Perl 5.12.</p>
</li>
<li><a name="*-_Matching_"></a><b><i>Matching</i></b>
<p>The key <code class="inline"><span class="q">&quot;~~&quot;</span></code>
 allows you to override the smart matching logic used by
the <code class="inline">~~</code>
 operator and the switch construct (<code class="inline">given</code>
/<code class="inline">when</code>
).  See
<a href="perlsyn.html#Switch-Statements">Switch Statements in perlsyn</a> and <a href="feature.html">feature</a>.</p>
<p>Unusually, the overloaded implementation of the smart match operator
does not get full control of the smart match behaviour.
In particular, in the following code:</p>
<pre class="verbatim"><ol><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;~~&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;match&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$obj</span> =  <span class="w">Foo</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$obj</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="s">]</span><span class="sc">;</span></li></ol></pre><p>the smart match does <i>not</i> invoke the method call like this:</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span><span class="i">-&gt;match</span><span class="s">(</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="s">]</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>rather, the smart match distributive rule takes precedence, so $obj is
smart matched against each array element in turn until a match is found,
so you may see between one and three of these calls instead:</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span><span class="i">-&gt;match</span><span class="s">(</span><span class="n">1</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$obj</span><span class="i">-&gt;match</span><span class="s">(</span><span class="n">2</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$obj</span><span class="i">-&gt;match</span><span class="s">(</span><span class="n">3</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Consult the match table in  <a href="perlop.html#Smartmatch-Operator">Smartmatch Operator in perlop</a> for
details of when overloading is invoked.</p>
</li>
<li><a name="*-_Dereferencing_"></a><b><i>Dereferencing</i></b>
<pre class="verbatim"><ol><li>    <span class="i">$</span>{}  <span class="i">@</span>{}  %{}  &amp;{}  *{}</li></ol></pre><p>If these operators are not explicitly overloaded then they
work in the normal way, yielding the underlying scalar,
array, or whatever stores the object data (or the appropriate
error message if the dereference operator doesn't match it).
Defining a catch-all <code class="inline"><span class="q">&#39;nomethod&#39;</span></code>
 (see <a href="#nomethod">below</a>)
makes no difference to this as the catch-all function will
not be called to implement a missing dereference operator.</p>
<p>If a dereference operator is overloaded then it must return a
<i>reference</i> of the appropriate type (for example, the
subroutine for key <code class="inline"><span class="q">&#39;${}&#39;</span></code>
 should return a reference to a
scalar, not a scalar), or another object which overloads the
operator: that is, the subroutine only determines what is
dereferenced and the actual dereferencing is left to Perl.
As a special case, if the subroutine returns the object itself
then it will not be called again - avoiding infinite recursion.</p>
</li>
<li><a name="*-_Special_"></a><b><i>Special</i></b>
<pre class="verbatim"><ol><li>    <span class="w">nomethod</span>  <span class="w">fallback</span>  =</li></ol></pre><p>See <a href="#Special-Keys-for-use-overload">Special Keys for use overload</a>.</p>
</li>
</ul>
<a name="Magic-Autogeneration"></a><h2>Magic Autogeneration</h2>
<p>If a method for an operation is not found then Perl tries to
autogenerate a substitute implementation from the operations
that have been defined.</p>
<p>Note: the behaviour described in this section can be disabled
by setting <code class="inline"><span class="w">fallback</span></code>
 to FALSE (see <a href="#fallback">fallback</a>).</p>
<p>In the following tables, numbers indicate priority.
For example, the table below states that,
if no implementation for <code class="inline"><span class="q">&#39;!&#39;</span></code>
 has been defined then Perl will
implement it using <code class="inline"><span class="q">&#39;bool&#39;</span></code>
 (that is, by inverting the value
returned by the method for <code class="inline"><span class="q">&#39;bool&#39;</span></code>
);
if boolean conversion is also unimplemented then Perl will
use <code class="inline"><span class="q">&#39;0+&#39;</span></code>
 or, failing that, <code class="inline"><span class="q">&#39;&quot;&quot;&#39;</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="w">operator</span> | <span class="w">can</span> <span class="w">be</span> <span class="w">autogenerated</span> <span class="w">from</span></li><li>             |</li><li>             | <span class="n">0</span>+   <span class="q">&quot;&quot;</span>   <span class="w">bool</span>   .   <span class="w">x</span></li><li>    =========|==========================</li><li>       <span class="n">0</span>+    |       <span class="n">1</span>     <span class="n">2</span></li><li>       <span class="q">&quot;&quot;</span>    |  <span class="n">1</span>          <span class="n">2</span></li><li>       <span class="w">bool</span>  |  <span class="n">1</span>    <span class="n">2</span></li><li>       <a class="l_k" href="functions/int.html">int</a>   |  <span class="n">1</span>    <span class="n">2</span>     <span class="n">3</span></li><li>       !     |  <span class="n">2</span>    <span class="n">3</span>     <span class="n">1</span></li><li>       <span class="q">qr    |  2    1     3</span></li><li>       <span class="q">       .     |</span>  <span class="n">2</span>    <span class="n">1</span>     <span class="n">3</span></li><li>       x     |  <span class="n">2</span>    <span class="n">1</span>     <span class="n">3</span></li><li>       .=    |  <span class="n">3</span>    <span class="n">2</span>     <span class="n">4</span>    <span class="n">1</span></li><li>       x=    |  <span class="n">3</span>    <span class="n">2</span>     <span class="n">4</span>        <span class="n">1</span></li><li>       &lt;&gt;    |  <span class="n">2</span>    <span class="n">1</span>     <span class="n">3</span></li><li>       -<span class="w">X</span>    |  <span class="n">2</span>    <span class="n">1</span>     <span class="n">3</span></li></ol></pre><p>Note: The iterator (<code class="inline"><span class="q">&#39;&lt;&gt;&#39;</span></code>
) and file test (<code class="inline"><span class="q">&#39;-X&#39;</span></code>
)
operators work as normal: if the operand is not a blessed glob or
IO reference then it is converted to a string (using the method
for <code class="inline"><span class="q">&#39;&quot;&quot;&#39;</span></code>
, <code class="inline"><span class="q">&#39;0+&#39;</span></code>
, or <code class="inline"><span class="q">&#39;bool&#39;</span></code>
) to be interpreted as a glob
or filename.</p>
<pre class="verbatim"><ol><li>    <span class="w">operator</span> | <span class="w">can</span> <span class="w">be</span> <span class="w">autogenerated</span> <span class="w">from</span></li><li>             |</li><li>             |  <span class="q">&lt;   &lt;=&gt;</span>   <span class="w">neg</span>   -=    -</li><li>    =========|==========================</li><li>       <span class="w">neg</span>   |                        <span class="n">1</span></li><li>       -=    |                        <span class="n">1</span></li><li>       --    |                   <span class="n">1</span>    <span class="n">2</span></li><li>       <a class="l_k" href="functions/abs.html">abs</a>   | <span class="w">a1</span>    <span class="w">a2</span>    <span class="w">b1</span>        <span class="w">b2</span>    <span class="s">[</span><span class="i">*]</span></li><li>       &lt;     |        <span class="n">1</span></li><li>       &lt;=    |        <span class="n">1</span></li><li>       &gt;     |        <span class="n">1</span></li><li>       &gt;=    |        <span class="n">1</span></li><li>       ==    |        <span class="n">1</span></li><li>       !=    |        <span class="n">1</span></li><li></li><li>    * <span class="w">one</span> <span class="w">from</span> <span class="s">[</span><span class="w">a1</span><span class="cm">,</span> <span class="w">a2</span><span class="s">]</span> and <span class="w">one</span> <span class="w">from</span> <span class="s">[</span><span class="w">b1</span><span class="cm">,</span> <span class="w">b2</span><span class="s">]</span></li></ol></pre><p>Just as numeric comparisons can be autogenerated from the method
for <code class="inline"><span class="q">&#39;&lt;=&gt;&#39;</span></code>
, string comparisons can be autogenerated from
that for <code class="inline"><span class="q">&#39;cmp&#39;</span></code>
:</p>
<pre class="verbatim"><ol><li>     <span class="w">operators</span>          |  <span class="w">can</span> <span class="w">be</span> <span class="w">autogenerated</span> <span class="w">from</span></li><li>    ====================|===========================</li><li>     lt gt le ge eq ne  |  cmp</li></ol></pre><p>Similarly, autogeneration for keys <code class="inline"><span class="q">&#39;+=&#39;</span></code>
 and <code class="inline"><span class="q">&#39;++&#39;</span></code>
 is analogous
to <code class="inline"><span class="q">&#39;-=&#39;</span></code>
 and <code class="inline"><span class="q">&#39;--&#39;</span></code>
 above:</p>
<pre class="verbatim"><ol><li>    <span class="w">operator</span> | <span class="w">can</span> <span class="w">be</span> <span class="w">autogenerated</span> <span class="w">from</span></li><li>             |</li><li>             |  +=    +</li><li>    =========|==========================</li><li>        +=   |        <span class="n">1</span></li><li>        ++   |   <span class="n">1</span>    <span class="n">2</span></li></ol></pre><p>And other assignment variations are analogous to
<code class="inline"><span class="q">&#39;+=&#39;</span></code>
 and <code class="inline"><span class="q">&#39;-=&#39;</span></code>
 (and similar to <code class="inline"><span class="q">&#39;.=&#39;</span></code>
 and <code class="inline"><span class="q">&#39;x=&#39;</span></code>
 above):</p>
<pre class="verbatim"><ol><li>              <span class="w">operator</span> ||  <span class="i">*=</span> /= %= <span class="i">**</span>= &lt;&lt;= &gt;&gt;= &amp;= ^= |= <span class="i">&amp;.</span>= ^.= |.=</li><li>    -------------------||-------------------------------------------</li><li>    <span class="w">autogenerated</span> <span class="w">from</span> ||  <span class="i">*  /</span>  %  <span class="i">**</span>  &lt;&lt;  &gt;&gt;  <span class="i">&amp;  ^</span>  |  <span class="i">&amp;.</span>  ^.  |.</li></ol></pre><p>Note also that the copy constructor (key <code class="inline"><span class="q">&#39;=&#39;</span></code>
) may be
autogenerated, but only for objects based on scalars.
See <a href="#Copy-Constructor">Copy Constructor</a>.</p>
<a name="Minimal-Set-of-Overloaded-Operations"></a><h3>Minimal Set of Overloaded Operations</h3>
<p>Since some operations can be automatically generated from others, there is
a minimal set of operations that need to be overloaded in order to have
the complete set of overloaded operations at one's disposal.
Of course, the autogenerated operations may not do exactly what the user
expects.  The minimal set is:</p>
<pre class="verbatim"><ol><li>    + - <span class="i">* /</span> % <span class="i">**</span> &lt;&lt; &gt;&gt; <span class="w">x</span></li><li>    &lt;=&gt; <a class="l_k" href="functions/cmp.html">cmp</a></li><li>    <span class="i">&amp; |</span> ^ ~ <span class="i">&amp;.</span> |. ^. ~.</li><li>    <a class="l_k" href="functions/atan2.html">atan2</a> <a class="l_k" href="functions/cos.html">cos</a> <a class="l_k" href="functions/sin.html">sin</a> <a class="l_k" href="functions/exp.html">exp</a> <a class="l_k" href="functions/log.html">log</a> <a class="l_k" href="functions/sqrt.html">sqrt</a> <a class="l_k" href="functions/int.html">int</a></li><li>    <span class="q">&quot;&quot;</span> <span class="n">0</span>+ <span class="w">bool</span></li><li>    ~~</li></ol></pre><p>Of the conversions, only one of string, boolean or numeric is
needed because each can be generated from either of the other two.</p>
<a name="Special-Keys-for-use-overload"></a><h2>Special Keys for <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
</h2>
<a name="nomethod"></a><h3><code class="inline"><span class="w">nomethod</span></code>
</h3>
<p>The <code class="inline"><span class="q">&#39;nomethod&#39;</span></code>
 key is used to specify a catch-all function to
be called for any operator that is not individually overloaded.
The specified function will be passed four parameters.
The first three arguments coincide with those that would have been
passed to the corresponding method if it had been defined.
The fourth argument is the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 key for that missing
method.  If the experimental "bitwise" feature is enabled (see <a href="feature.html">feature</a>),
a fifth TRUE argument is passed to subroutines handling <code class="inline"><span class="i">&amp;</span></code>
, <code class="inline">|</code>, <code class="inline">^</code> and <code class="inline">~</code>
 to indicate that the caller is expecting numeric behaviour.</p>
<p>For example, if <code class="inline"><span class="i">$a</span></code>
 is an object blessed into a package declaring</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;nomethod&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;catch_all&#39;</span><span class="cm">,</span> <span class="c"># ...</span></li></ol></pre><p>then the operation</p>
<pre class="verbatim"><ol><li>    <span class="n">3</span> + <span class="i">$a</span></li></ol></pre><p>could (unless a method is specifically declared for the key
<code class="inline"><span class="q">&#39;+&#39;</span></code>
) result in a call</p>
<pre class="verbatim"><ol><li>    <span class="i">catch_all</span><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="q">&#39;+&#39;</span><span class="s">)</span></li></ol></pre><p>See <a href="#How-Perl-Chooses-an-Operator-Implementation">How Perl Chooses an Operator Implementation</a>.</p>
<a name="fallback"></a><h3><code class="inline"><span class="w">fallback</span></code>
</h3>
<p>The value assigned to the key <code class="inline"><span class="q">&#39;fallback&#39;</span></code>
 tells Perl how hard
it should try to find an alternative way to implement a missing
operator.</p>
<ul>
<li><a name="*-defined%2c-but-FALSE"></a><b>defined, but FALSE</b>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&quot;fallback&quot;</span> <span class="cm">=&gt;</span> <span class="n">0</span><span class="cm">,</span> <span class="c"># ... ;</span></li></ol></pre><p>This disables <a href="#Magic-Autogeneration">Magic Autogeneration</a>.</p>
</li>
<li><a name="*-undef"></a><b><code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code></b>
<p>In the default case where no value is explicitly assigned to
<code class="inline"><span class="w">fallback</span></code>
, magic autogeneration is enabled.</p>
</li>
<li><a name="*-TRUE"></a><b>TRUE</b>
<p>The same as for <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>, but if a missing operator cannot be
autogenerated then, instead of issuing an error message, Perl
is allowed to revert to what it would have done for that
operator if there had been no <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive.</p>
<p>Note: in most cases, particularly the <a href="#Copy-Constructor">Copy Constructor</a>,
this is unlikely to be appropriate behaviour.</p>
</li>
</ul>
<p>See <a href="#How-Perl-Chooses-an-Operator-Implementation">How Perl Chooses an Operator Implementation</a>.</p>
<a name="Copy-Constructor"></a><h3>Copy Constructor</h3>
<p>As mentioned <a href="#Mathemagic%2c-Mutators%2c-and-Copy-Constructors">above</a>,
this operation is called when a mutator is applied to a reference
that shares its object with some other reference.
For example, if <code class="inline"><span class="i">$b</span></code>
 is mathemagical, and <code class="inline"><span class="q">&#39;++&#39;</span></code>
 is overloaded
with <code class="inline"><span class="q">&#39;incr&#39;</span></code>
, and <code class="inline"><span class="q">&#39;=&#39;</span></code>
 is overloaded with <code class="inline"><span class="q">&#39;clone&#39;</span></code>
, then the
code</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> = <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="c"># ... (other code which does not modify $a or $b) ...</span></li><li>    ++<span class="i">$b</span><span class="sc">;</span></li></ol></pre><p>would be executed in a manner equivalent to</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> = <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="c"># ...</span></li><li>    <span class="i">$b</span> = <span class="i">$b</span><span class="i">-&gt;clone</span><span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="q">&quot;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$b</span><span class="i">-&gt;incr</span><span class="s">(</span><a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="q">&quot;&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note:</p>
<ul>
<li>
<p>The subroutine for <code class="inline"><span class="q">&#39;=&#39;</span></code>
 does not overload the Perl assignment
operator: it is used only to allow mutators to work as described
here.  (See <a href="#Assignments">Assignments</a> above.)</p>
</li>
<li>
<p>As for other operations, the subroutine implementing '=' is passed
three arguments, though the last two are always <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> and <code class="inline"><span class="q">&#39;&#39;</span></code>
.</p>
</li>
<li>
<p>The copy constructor is called only before a call to a function
declared to implement a mutator, for example, if <code class="inline">++<span class="i">$b</span><span class="sc">;</span></code>
 in the
code above is effected via a method declared for key <code class="inline"><span class="q">&#39;++&#39;</span></code>

(or 'nomethod', passed <code class="inline"><span class="q">&#39;++&#39;</span></code>
 as the fourth argument) or, by
autogeneration, <code class="inline"><span class="q">&#39;+=&#39;</span></code>
.
It is not called if the increment operation is effected by a call
to the method for <code class="inline"><span class="q">&#39;+&#39;</span></code>
 since, in the equivalent code,</p>
<pre class="verbatim"><ol><li>    <span class="i">$a</span> = <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="i">$b</span> + <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>the data referred to by <code class="inline"><span class="i">$a</span></code>
 is unchanged by the assignment to
<code class="inline"><span class="i">$b</span></code>
 of a reference to new object data.</p>
</li>
<li>
<p>The copy constructor is not called if Perl determines that it is
unnecessary because there is no other reference to the data being
modified.</p>
</li>
<li>
<p>If <code class="inline"><span class="q">&#39;fallback&#39;</span></code>
 is undefined or TRUE then a copy constructor
can be autogenerated, but only for objects based on scalars.
In other cases it needs to be defined explicitly.
Where an object's data is stored as, for example, an array of
scalars, the following might be appropriate:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;=&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span> <span class="i">@</span>{<span class="i">$_</span>[<span class="n">0</span>]} <span class="s">]</span> <span class="s">}</span><span class="cm">,</span>  <span class="c"># ...</span></li></ol></pre></li>
<li>
<p>If <code class="inline"><span class="q">&#39;fallback&#39;</span></code>
 is TRUE and no copy constructor is defined then,
for objects not based on scalars, Perl may silently fall back on
simple assignment - that is, assignment of the object reference.
In effect, this disables the copy constructor mechanism since
no new copy of the object data is created.
This is almost certainly not what you want.
(It is, however, consistent: for example, Perl's fallback for the
<code class="inline">++</code>
 operator is to increment the reference itself.)</p>
</li>
</ul>
<a name="How-Perl-Chooses-an-Operator-Implementation"></a><h2>How Perl Chooses an Operator Implementation</h2>
<p>Which is checked first, <code class="inline"><span class="w">nomethod</span></code>
 or <code class="inline"><span class="w">fallback</span></code>
?
If the two operands of an operator are of different types and
both overload the operator, which implementation is used?
The following are the precedence rules:</p>
<dl>
<dt>1.</dt><dd>
<p>If the first operand has declared a subroutine to overload the
operator then use that implementation.</p>
</dd>
<dt>2.</dt><dd>
<p>Otherwise, if fallback is TRUE or undefined for the
first operand then see if the
<a href="#Magic-Autogeneration">rules for autogeneration</a>
allows another of its operators to be used instead.</p>
</dd>
<dt>3.</dt><dd>
<p>Unless the operator is an assignment (<code class="inline">+=</code>
, <code class="inline">-=</code>
, etc.),
repeat step (1) in respect of the second operand.</p>
</dd>
<dt>4.</dt><dd>
<p>Repeat Step (2) in respect of the second operand.</p>
</dd>
<dt>5.</dt><dd>
<p>If the first operand has a "nomethod" method then use that.</p>
</dd>
<dt>6.</dt><dd>
<p>If the second operand has a "nomethod" method then use that.</p>
</dd>
<dt>7.</dt><dd>
<p>If <code class="inline"><span class="w">fallback</span></code>
 is TRUE for both operands
then perform the usual operation for the operator,
treating the operands as numbers, strings, or booleans
as appropriate for the operator (see note).</p>
</dd>
<dt>8.</dt><dd>
<p>Nothing worked - die.</p>
</dd>
</dl>
<p>Where there is only one operand (or only one operand with
overloading) the checks in respect of the other operand above are
skipped.</p>
<p>There are exceptions to the above rules for dereference operations
(which, if Step 1 fails, always fall back to the normal, built-in
implementations - see Dereferencing), and for <code class="inline">~~</code>
 (which has its
own set of rules - see <code class="inline"><span class="w">Matching</span></code>
 under <a href="#Overloadable-Operations">Overloadable Operations</a>
above).</p>
<p>Note on Step 7: some operators have a different semantic depending
on the type of their operands.
As there is no way to instruct Perl to treat the operands as, e.g.,
numbers instead of strings, the result here may not be what you
expect.
See <a href="#BUGS-AND-PITFALLS">BUGS AND PITFALLS</a>.</p>
<a name="Losing-Overloading"></a><h2>Losing Overloading</h2>
<p>The restriction for the comparison operation is that even if, for example,
<code class="inline">cmp</code>
 should return a blessed reference, the autogenerated <code class="inline">lt</code>

function will produce only a standard logical value based on the
numerical value of the result of <code class="inline">cmp</code>
.  In particular, a working
numeric conversion is needed in this case (possibly expressed in terms of
other conversions).</p>
<p>Similarly, <code class="inline">.=</code>
  and <code class="inline"><span class="w">x</span>=</code>
 operators lose their mathemagical properties
if the string conversion substitution is applied.</p>
<p>When you chop() a mathemagical object it is promoted to a string and its
mathemagical properties are lost.  The same can happen with other
operations as well.</p>
<a name="Inheritance-and-Overloading"></a><h2>Inheritance and Overloading</h2>
<p>Overloading respects inheritance via the @ISA hierarchy.
Inheritance interacts with overloading in two ways.</p>
<ul>
<li><a name="Method-names-in-the-use-overload-directive"></a><b>Method names in the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive</b>
<p>If <code class="inline"><span class="w">value</span></code>
 in</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="w">key</span> <span class="cm">=&gt;</span> <span class="w">value</span><span class="sc">;</span></li></ol></pre><p>is a string, it is interpreted as a method name - which may
(in the usual way) be inherited from another class.</p>
</li>
<li><a name="Overloading-of-an-operation-is-inherited-by-derived-classes"></a><b>Overloading of an operation is inherited by derived classes</b>
<p>Any class derived from an overloaded class is also overloaded
and inherits its operator implementations.
If the same operator is overloaded in more than one ancestor
then the implementation is determined by the usual inheritance
rules.</p>
<p>For example, if <code class="inline"><span class="w">A</span></code>
 inherits from <code class="inline"><span class="w">B</span></code>
 and <code class="inline"><span class="w">C</span></code>
 (in that order),
<code class="inline"><span class="w">B</span></code>
 overloads <code class="inline">+</code>
 with <code class="inline">\<span class="i">&amp;D::plus_sub</span></code>
, and <code class="inline"><span class="w">C</span></code>
 overloads
<code class="inline">+</code>
 by <code class="inline"><span class="q">&quot;plus_meth&quot;</span></code>
, then the subroutine <code class="inline"><span class="w">D::plus_sub</span></code>
 will
be called to implement operation <code class="inline">+</code>
 for an object in package <code class="inline"><span class="w">A</span></code>
.</p>
</li>
</ul>
<p>Note that in Perl version prior to 5.18 inheritance of the <code class="inline"><span class="w">fallback</span></code>
 key
was not governed by the above rules.  The value of <code class="inline"><span class="w">fallback</span></code>
 in the first 
overloaded ancestor was used.  This was fixed in 5.18 to follow the usual
rules of inheritance.</p>
<a name="Run-time-Overloading"></a><h2>Run-time Overloading</h2>
<p>Since all <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> directives are executed at compile-time, the only way to
change overloading during run-time is to</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&#39;use overload &quot;+&quot; =&gt; \&amp;addmethod&#39;</span><span class="sc">;</span></li></ol></pre><p>You can also use</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&#39;no overload &quot;+&quot;, &quot;--&quot;, &quot;&lt;=&quot;&#39;</span><span class="sc">;</span></li></ol></pre><p>though the use of these constructs during run-time is questionable.</p>
<a name="Public-Functions"></a><h2>Public Functions</h2>
<p>Package <code class="inline"><span class="w">overload</span>.<span class="w">pm</span></code>
 provides the following public functions:</p>
<ul>
<li><a name="overload%3a%3aStrVal(arg)"></a><b>overload::StrVal(arg)</b>
<p>Gives the string value of <code class="inline"><span class="w">arg</span></code>
 as in the
absence of stringify overloading.  If you
are using this to get the address of a reference (useful for checking if two
references point to the same thing) then you may be better off using
<code class="inline"><span class="i">Scalar::Util::refaddr</span><span class="s">(</span><span class="s">)</span></code>
, which is faster.</p>
</li>
<li><a name="overload%3a%3aOverloaded(arg)"></a><b>overload::Overloaded(arg)</b>
<p>Returns true if <code class="inline"><span class="w">arg</span></code>
 is subject to overloading of some operations.</p>
</li>
<li><a name="overload%3a%3aMethod(obj%2cop)"></a><b>overload::Method(obj,op)</b>
<p>Returns <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> or a reference to the method that implements <code class="inline"><span class="w">op</span></code>
.</p>
</li>
</ul>
<a name="Overloading-Constants"></a><h2>Overloading Constants</h2>
<p>For some applications, the Perl parser mangles constants too much.
It is possible to hook into this process via <code class="inline"><span class="i">overload::constant</span><span class="s">(</span><span class="s">)</span></code>

and <code class="inline"><span class="i">overload::remove_constant</span><span class="s">(</span><span class="s">)</span></code>
 functions.</p>
<p>These functions take a hash as an argument.  The recognized keys of this hash
are:</p>
<ul>
<li><a name="integer"></a><b>integer</b>
<p>to overload integer constants,</p>
</li>
<li><a name="float"></a><b>float</b>
<p>to overload floating point constants,</p>
</li>
<li><a name="binary"></a><b>binary</b>
<p>to overload octal and hexadecimal constants,</p>
</li>
<li><a name="q"></a><b>q</b>
<p>to overload <code class="inline"><a class="l_k" href="functions/q.html">q</a></code>-quoted strings, constant pieces of <code class="inline"><a class="l_k" href="functions/qq.html">qq</a></code>- and <code class="inline"><a class="l_k" href="functions/qx.html">qx</a></code>-quoted
strings and here-documents,</p>
</li>
<li><a name="qr"></a><b>qr</b>
<p>to overload constant pieces of regular expressions.</p>
</li>
</ul>
<p>The corresponding values are references to functions which take three arguments:
the first one is the <i>initial</i> string form of the constant, the second one
is how Perl interprets this constant, the third one is how the constant is used.
Note that the initial string form does not
contain string delimiters, and has backslashes in backslash-delimiter
combinations stripped (thus the value of delimiter is not relevant for
processing of this string).  The return value of this function is how this
constant is going to be interpreted by Perl.  The third argument is undefined
unless for overloaded <code class="inline"><a class="l_k" href="functions/q.html">q</a></code>- and <code class="inline"><a class="l_k" href="functions/qr.html">qr</a></code>- constants, it is <code class="inline"><a class="l_k" href="functions/q.html">q</a></code> in single-quote
context (comes from strings, regular expressions, and single-quote HERE
documents), it is <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> for arguments of <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code>/<code class="inline"><a class="l_k" href="functions/y.html">y</a></code> operators,
it is <code class="inline"><a class="l_k" href="functions/s.html">s</a></code> for right-hand side of <code class="inline"><a class="l_k" href="functions/s.html">s</a></code>-operator, and it is <code class="inline"><a class="l_k" href="functions/qq.html">qq</a></code> otherwise.</p>
<p>Since an expression <code class="inline"><span class="q">&quot;ab$cd,,&quot;</span></code>
 is just a shortcut for <code class="inline"><span class="q">&#39;ab&#39;</span> . <span class="i">$cd</span> . <span class="q">&#39;,,&#39;</span></code>
,
it is expected that overloaded constant strings are equipped with reasonable
overloaded catenation operator, otherwise absurd results will result.
Similarly, negative numbers are considered as negations of positive constants.</p>
<p>Note that it is probably meaningless to call the functions overload::constant()
and overload::remove_constant() from anywhere but import() and unimport() methods.
From these methods they may be called as</p>
<pre class="verbatim"><ol><li><a name="import"></a>    sub <span class="m">import</span> <span class="s">{</span></li><li>       <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> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">@_</span><span class="sc">;</span></li><li>       <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;unknown import: @_&quot;</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">@_</span> == <span class="n">1</span> <a class="l_k" href="functions/and.html">and</a> <span class="i">$_</span>[<span class="n">0</span>] <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;:constant&#39;</span><span class="sc">;</span></li><li>       <span class="w">overload::constant</span> <span class="w">integer</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><span class="w">Math::BigInt</span><span class="w">-&gt;new</span><span class="s">(</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">)</span><span class="s">}</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><a name="IMPLEMENTATION"></a><h1>IMPLEMENTATION</h1>
<p>What follows is subject to change RSN.</p>
<p>The table of methods for all operations is cached in magic for the
symbol table hash for the package.  The cache is invalidated during
processing of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
, <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">overload</span></code>
, new function
definitions, and changes in @ISA.</p>
<p>(Every SVish thing has a magic queue, and magic is an entry in that
queue.  This is how a single variable may participate in multiple
forms of magic simultaneously.  For instance, environment variables
regularly have two forms at once: their %ENV magic and their taint
magic.  However, the magic which implements overloading is applied to
the stashes, which are rarely used directly, thus should not slow down
Perl.)</p>
<p>If a package uses overload, it carries a special flag.  This flag is also
set when new functions are defined or @ISA is modified.  There will be a
slight speed penalty on the very first operation thereafter that supports
overloading, while the overload tables are updated.  If there is no
overloading present, the flag is turned off.  Thus the only speed penalty
thereafter is the checking of this flag.</p>
<p>It is expected that arguments to methods that are not explicitly supposed
to be changed are constant (but this is not enforced).</p>
<a name="COOKBOOK"></a><h1>COOKBOOK</h1>
<p>Please add examples to what follows!</p>
<a name="Two-face-Scalars"></a><h2>Two-face Scalars</h2>
<p>Put this in <i>two_face.pm</i> in your Perl library directory:</p>
<pre class="verbatim"><ol><li><a name="package-two_face"></a>  package <span class="i">two_face</span><span class="sc">;</span>		<span class="c"># Scalars with separate string and</span></li><li>                                <span class="c"># numeric values.</span></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="i">@_</span><span class="s">]</span><span class="cm">,</span> <span class="i">$p</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;&quot;&quot;&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;str</span><span class="cm">,</span> <span class="q">&#39;0+&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;num</span><span class="cm">,</span> <span class="w">fallback</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="sc">;</span></li><li><a name="num"></a>  sub <span class="m">num</span> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a>-&gt;[<span class="n">1</span>]<span class="s">}</span></li><li><a name="str"></a>  sub <span class="m">str</span> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a>-&gt;[<span class="n">0</span>]<span class="s">}</span></li></ol></pre><p>Use it as follows:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/require.html">require</a> <span class="w">two_face</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$seven</span> = <span class="w">two_face</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;vii&quot;</span><span class="cm">,</span> <span class="n">7</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;seven=$seven, seven=%d, eight=%d\n&quot;</span><span class="cm">,</span> <span class="i">$seven</span><span class="cm">,</span> <span class="i">$seven</span>+<span class="n">1</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;seven contains &#39;i&#39;\n&quot;</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$seven</span> =~ <span class="q">/i/</span><span class="sc">;</span></li></ol></pre><p>(The second line creates a scalar which has both a string value, and a
numeric value.)  This prints:</p>
<pre class="verbatim"><ol><li>  <span class="w">seven</span>=<span class="w">vii</span><span class="cm">,</span> <span class="w">seven</span>=<span class="n">7</span><span class="cm">,</span> <span class="w">eight</span>=<span class="n">8</span></li><li>  <span class="w">seven</span> <span class="w">contains</span> <span class="q">&#39;i&#39;</span></li></ol></pre><a name="Two-face-References"></a><h2>Two-face References</h2>
<p>Suppose you want to create an object which is accessible as both an
array reference and a hash reference.</p>
<pre class="verbatim"><ol><li><a name="package-two_refs"></a>  package <span class="i">two_refs</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;%{}&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;gethash</span><span class="cm">,</span> <span class="q">&#39;@{}&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$</span> {<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>} <span class="s">}</span><span class="sc">;</span></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> \ <span class="s">[</span><span class="i">@_</span><span class="s">]</span><span class="cm">,</span> <span class="i">$p</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li><a name="gethash"></a>  sub <span class="m">gethash</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%h</span><span class="cm">,</span> <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="cm">,</span> <span class="i">$self</span><span class="sc">;</span></li><li>    \<span class="i">%h</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="TIEHASH"></a>  sub <span class="m">TIEHASH</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> \ <a class="l_k" href="functions/shift.html">shift</a><span class="cm">,</span> <span class="i">$p</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">%fields</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span></li><li>  <span class="i">$fields</span>{<span class="i">$_</span>} = <span class="i">$i</span>++ foreach <span class="q">qw{zero one two three}</span><span class="sc">;</span></li><li><a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="i">$fields</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$key</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Out of band access&quot;</span><span class="sc">;</span></li><li>    <span class="i">$$self</span>-&gt;[<span class="i">$key</span>] = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>  <span class="s">}</span></li><li><a name="FETCH"></a>  sub <span class="m">FETCH</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="i">$fields</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$key</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Out of band access&quot;</span><span class="sc">;</span></li><li>    <span class="i">$$self</span>-&gt;[<span class="i">$key</span>]<span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Now one can access an object using both the array and hash syntax:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$bar</span> = <span class="w">two_refs</span><span class="w">-&gt;new</span><span class="s">(</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="s">)</span><span class="sc">;</span></li><li>  <span class="i">$bar</span>-&gt;[<span class="n">2</span>] = <span class="n">11</span><span class="sc">;</span></li><li>  <span class="i">$bar</span>-&gt;{<span class="w">two</span>} == <span class="n">11</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&#39;bad hash fetch&#39;</span><span class="sc">;</span></li></ol></pre><p>Note several important features of this example.  First of all, the
<i>actual</i> type of $bar is a scalar reference, and we do not overload
the scalar dereference.  Thus we can get the <i>actual</i> non-overloaded
contents of $bar by just using <code class="inline"><span class="i">$$bar</span></code>
 (what we do in functions which
overload dereference).  Similarly, the object returned by the
TIEHASH() method is a scalar reference.</p>
<p>Second, we create a new tied hash each time the hash syntax is used.
This allows us not to worry about a possibility of a reference loop,
which would lead to a memory leak.</p>
<p>Both these problems can be cured.  Say, if we want to overload hash
dereference on a reference to an object which is <i>implemented</i> as a
hash itself, the only problem one has to circumvent is how to access
this <i>actual</i> hash (as opposed to the <i>virtual</i> hash exhibited by the
overloaded dereference operator).  Here is one possible fetching routine:</p>
<pre class="verbatim"><ol><li><a name="access_hash"></a>  sub <span class="m">access_hash</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="i">$key</span><span class="s">)</span> = <span class="s">(</span><a class="l_k" href="functions/shift.html">shift</a><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</span> = <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> <span class="i">$self</span><span class="cm">,</span> <span class="q">&#39;overload::dummy&#39;</span><span class="sc">;</span> <span class="c"># Disable overloading of %{}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$out</span> = <span class="i">$self</span>-&gt;{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> <span class="i">$self</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span>	<span class="c"># Restore overloading</span></li><li>    <span class="i">$out</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>To remove creation of the tied hash on each access, one may an extra
level of indirection which allows a non-circular structure of references:</p>
<pre class="verbatim"><ol><li><a name="package-two_refs1"></a>  package <span class="i">two_refs1</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;%{}&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}-&gt;[<span class="n">1</span>] <span class="s">}</span><span class="cm">,</span></li><li>               <span class="q">&#39;@{}&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}-&gt;[<span class="n">0</span>] <span class="s">}</span><span class="sc">;</span></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</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">$a</span> = <span class="s">[</span><span class="i">@_</span><span class="s">]</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%h</span><span class="cm">,</span> <span class="i">$p</span><span class="cm">,</span> <span class="i">$a</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> \ <span class="s">[</span><span class="i">$a</span><span class="cm">,</span> \<span class="i">%h</span><span class="s">]</span><span class="cm">,</span> <span class="i">$p</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li><a name="gethash"></a>  sub <span class="m">gethash</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/tie.html">tie</a> <span class="i">%h</span><span class="cm">,</span> <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="cm">,</span> <span class="i">$self</span><span class="sc">;</span></li><li>    \<span class="i">%h</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li></li><li><a name="TIEHASH"></a>  sub <span class="m">TIEHASH</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> \ <a class="l_k" href="functions/shift.html">shift</a><span class="cm">,</span> <span class="i">$p</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">%fields</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span></li><li>  <span class="i">$fields</span>{<span class="i">$_</span>} = <span class="i">$i</span>++ foreach <span class="q">qw{zero one two three}</span><span class="sc">;</span></li><li><a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="i">$fields</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$key</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Out of band access&quot;</span><span class="sc">;</span></li><li>    <span class="i">$a</span>-&gt;[<span class="i">$key</span>] = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>  <span class="s">}</span></li><li><a name="FETCH"></a>  sub <span class="m">FETCH</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="i">$</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="i">$fields</span>{<a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$key</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Out of band access&quot;</span><span class="sc">;</span></li><li>    <span class="i">$a</span>-&gt;[<span class="i">$key</span>]<span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Now if $baz is overloaded like this, then <code class="inline"><span class="i">$baz</span></code>
 is a reference to a
reference to the intermediate array, which keeps a reference to an
actual array, and the access hash.  The tie()ing object for the access
hash is a reference to a reference to the actual array, so</p>
<ul>
<li>
<p>There are no loops of references.</p>
</li>
<li>
<p>Both "objects" which are blessed into the class <code class="inline"><span class="w">two_refs1</span></code>
 are
references to a reference to an array, thus references to a <i>scalar</i>.
Thus the accessor expression <code class="inline"><span class="i">$$foo</span>-&gt;[<span class="i">$ind</span>]</code>
 involves no
overloaded operations.</p>
</li>
</ul>
<a name="Symbolic-Calculator"></a><h2>Symbolic Calculator</h2>
<p>Put this in <i>symbolic.pm</i> in your Perl library directory:</p>
<pre class="verbatim"><ol><li><a name="package-symbolic"></a>  package <span class="i">symbolic</span><span class="sc">;</span>		<span class="c"># Primitive symbolic calculator</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="w">nomethod</span> <span class="cm">=&gt;</span> \<span class="i">&amp;wrap</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span> <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="i">@_</span><span class="s">]</span> <span class="s">}</span></li><li><a name="wrap"></a>  sub <span class="m">wrap</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="cm">,</span> <span class="i">$inv</span><span class="cm">,</span> <span class="i">$meth</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="s">)</span> = <span class="s">(</span><span class="i">$other</span><span class="cm">,</span> <span class="i">$obj</span><span class="s">)</span> if <span class="i">$inv</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="s">]</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>This module is very unusual as overloaded modules go: it does not
provide any usual overloaded operators, instead it provides an
implementation for <a href="#nomethod">nomethod</a>.  In this example the <code class="inline"><span class="w">nomethod</span></code>

subroutine returns an object which encapsulates operations done over
the objects: <code class="inline"><span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
 contains <code class="inline"><span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="n">3</span><span class="s">]</span></code>
, <code class="inline"><span class="n">2</span> +
<span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">3</span><span class="s">)</span></code>
 contains <code class="inline"><span class="s">[</span><span class="q">&#39;+&#39;</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="n">3</span><span class="s">]</span><span class="s">]</span></code>
.</p>
<p>Here is an example of the script which "calculates" the side of
circumscribed octagon using the above package:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/require.html">require</a> <span class="w">symbolic</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$iter</span> = <span class="n">1</span><span class="sc">;</span>			<span class="c"># 2**($iter+2) = 8</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$side</span> = <span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$cnt</span> = <span class="i">$iter</span><span class="sc">;</span></li><li></li><li>  while <span class="s">(</span><span class="i">$cnt</span>--<span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$side</span> = <span class="s">(</span><a class="l_k" href="functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> + <span class="i">$side</span>**<span class="n">2</span><span class="s">)</span> - <span class="n">1</span><span class="s">)</span>/<span class="i">$side</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;OK\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The value of $side is</p>
<pre class="verbatim"><ol><li>  <span class="s">[</span><span class="q">&#39;/&#39;</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;-&#39;</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;sqrt&#39;</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;+&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;**&#39;</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="s">]</span><span class="cm">,</span> <span class="n">2</span><span class="s">]</span><span class="s">]</span><span class="cm">,</span></li><li>	               <a class="l_k" href="functions/undef.html">undef</a><span class="s">]</span><span class="cm">,</span> <span class="n">1</span><span class="s">]</span><span class="cm">,</span> <span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="s">]</span><span class="s">]</span></li></ol></pre><p>Note that while we obtained this value using a nice little script,
there is no simple way to <i>use</i> this value.  In fact this value may
be inspected in debugger (see <a href="perldebug.html">perldebug</a>), but only if
<code class="inline"><span class="w">bareStringify</span></code>
 <b>O</b>ption is set, and not via <code class="inline"><span class="w">p</span></code>
 command.</p>
<p>If one attempts to print this value, then the overloaded operator
<code class="inline"><span class="q">&quot;&quot;</span></code>
 will be called, which will call <code class="inline"><span class="w">nomethod</span></code>
 operator.  The
result of this operator will be stringified again, but this result is
again of type <code class="inline"><span class="w">symbolic</span></code>
, which will lead to an infinite loop.</p>
<p>Add a pretty-printer method to the module <i>symbolic.pm</i>:</p>
<pre class="verbatim"><ol><li><a name="pretty"></a>  sub <span class="m">pretty</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@</span>{+<a class="l_k" href="functions/shift.html">shift</a>}<span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="q">&#39;u&#39;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$a</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="q">&#39;u&#39;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="i">$a</span><span class="i">-&gt;pretty</span> if <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$a</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="i">$b</span><span class="i">-&gt;pretty</span> if <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Now one can finish the script by</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;side = &quot;</span><span class="cm">,</span> <span class="i">$side</span><span class="i">-&gt;pretty</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The method <code class="inline"><span class="w">pretty</span></code>
 is doing object-to-string conversion, so it
is natural to overload the operator <code class="inline"><span class="q">&quot;&quot;</span></code>
 using this method.  However,
inside such a method it is not necessary to pretty-print the
<i>components</i> $a and $b of an object.  In the above subroutine
<code class="inline"><span class="q">&quot;[$meth $a $b]&quot;</span></code>
 is a catenation of some strings and components $a
and $b.  If these components use overloading, the catenation operator
will look for an overloaded operator <code class="inline">.</code>; if not present, it will
look for an overloaded operator <code class="inline"><span class="q">&quot;&quot;</span></code>
.  Thus it is enough to use</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="w">nomethod</span> <span class="cm">=&gt;</span> \<span class="i">&amp;wrap</span><span class="cm">,</span> <span class="q">&#39;&quot;&quot;&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;str</span><span class="sc">;</span></li><li><a name="str"></a>  sub <span class="m">str</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@</span>{+<a class="l_k" href="functions/shift.html">shift</a>}<span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="q">&#39;u&#39;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$a</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="q">&#39;u&#39;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Now one can change the last line of the script to</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;side = $side\n&quot;</span><span class="sc">;</span></li></ol></pre><p>which outputs</p>
<pre class="verbatim"><ol><li>  side = [/ [- [sqrt [+ 1 [** [n 1 u] 2]] u] 1] [n 1 u]]</li></ol></pre><p>and one can inspect the value in debugger using all the possible
methods.</p>
<p>Something is still amiss: consider the loop variable $cnt of the
script.  It was a number, not an object.  We cannot make this value of
type <code class="inline"><span class="w">symbolic</span></code>
, since then the loop will not terminate.</p>
<p>Indeed, to terminate the cycle, the $cnt should become false.
However, the operator <code class="inline"><span class="w">bool</span></code>
 for checking falsity is overloaded (this
time via overloaded <code class="inline"><span class="q">&quot;&quot;</span></code>
), and returns a long string, thus any object
of type <code class="inline"><span class="w">symbolic</span></code>
 is true.  To overcome this, we need a way to
compare an object to 0.  In fact, it is easier to write a numeric
conversion routine.</p>
<p>Here is the text of <i>symbolic.pm</i> with such a routine added (and
slightly modified str()):</p>
<pre class="verbatim"><ol><li><a name="package-symbolic"></a>  package <span class="i">symbolic</span><span class="sc">;</span>		<span class="c"># Primitive symbolic calculator</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></li><li>    <span class="w">nomethod</span> <span class="cm">=&gt;</span> \<span class="i">&amp;wrap</span><span class="cm">,</span> <span class="q">&#39;&quot;&quot;&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;str</span><span class="cm">,</span> <span class="q">&#39;0+&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;num</span><span class="sc">;</span></li><li></li><li><a name="new"></a>  sub <span class="m">new</span> <span class="s">{</span> <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="q">&#39;n&#39;</span><span class="cm">,</span> <span class="i">@_</span><span class="s">]</span> <span class="s">}</span></li><li><a name="wrap"></a>  sub <span class="m">wrap</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="cm">,</span> <span class="i">$inv</span><span class="cm">,</span> <span class="i">$meth</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="s">)</span> = <span class="s">(</span><span class="i">$other</span><span class="cm">,</span> <span class="i">$obj</span><span class="s">)</span> if <span class="i">$inv</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$obj</span><span class="cm">,</span> <span class="i">$other</span><span class="s">]</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li><a name="str"></a>  sub <span class="m">str</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@</span>{+<a class="l_k" href="functions/shift.html">shift</a>}<span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="q">&#39;u&#39;</span> unless <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$a</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a> <span class="i">$b</span><span class="s">)</span> <span class="s">{</span></li><li>      <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> else <span class="s">{</span></li><li>      <span class="q">&quot;[$meth $a]&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">%subr</span> = <span class="s">(</span> <span class="w">n</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">}</span><span class="cm">,</span></li><li>	       <span class="w">sqrt</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/sqrt.html">sqrt</a> <span class="i">$_</span>[<span class="n">0</span>]<span class="s">}</span><span class="cm">,</span></li><li>	       <span class="q">&#39;-&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> - <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span><span class="s">}</span><span class="cm">,</span></li><li>	       <span class="q">&#39;+&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> + <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span><span class="s">}</span><span class="cm">,</span></li><li>	       <span class="q">&#39;/&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> / <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span><span class="s">}</span><span class="cm">,</span></li><li>	       <span class="q">&#39;*&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> * <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span><span class="s">}</span><span class="cm">,</span></li><li>	       <span class="q">&#39;**&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> ** <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span><span class="s">}</span><span class="cm">,</span></li><li>	     <span class="s">)</span><span class="sc">;</span></li><li><a name="num"></a>  sub <span class="m">num</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$meth</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">@</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">$subr</span> = <span class="i">$subr</span>{<span class="i">$meth</span>}</li><li>      or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Do not know how to ($meth) in symbolic&quot;</span><span class="sc">;</span></li><li>    <span class="i">$a</span> = <span class="i">$a</span><span class="i">-&gt;num</span> if <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$a</span> eq <span class="w">__PACKAGE__</span><span class="sc">;</span></li><li>    <span class="i">$b</span> = <span class="i">$b</span><span class="i">-&gt;num</span> if <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$b</span> eq <span class="w">__PACKAGE__</span><span class="sc">;</span></li><li>    <span class="i">$subr</span>-&gt;<span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>All the work of numeric conversion is done in %subr and num().  Of
course, %subr is not complete, it contains only operators used in the
example below.  Here is the extra-credit question: why do we need an
explicit recursion in num()?  (Answer is at the end of this section.)</p>
<p>Use this module like this:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/require.html">require</a> <span class="w">symbolic</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$iter</span> = <span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">2</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># 16-gon</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$side</span> = <span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$cnt</span> = <span class="i">$iter</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span><span class="i">$cnt</span><span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$cnt</span> = <span class="i">$cnt</span> - <span class="n">1</span><span class="sc">;</span>		<span class="c"># Mutator &#39;--&#39; not implemented</span></li><li>    <span class="i">$side</span> = <span class="s">(</span><a class="l_k" href="functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> + <span class="i">$side</span>**<span class="n">2</span><span class="s">)</span> - <span class="n">1</span><span class="s">)</span>/<span class="i">$side</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;%s=%f\n&quot;</span><span class="cm">,</span> <span class="i">$side</span><span class="cm">,</span> <span class="i">$side</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;pi=%f\n&quot;</span><span class="cm">,</span> <span class="i">$side</span>*<span class="s">(</span><span class="n">2</span>**<span class="s">(</span><span class="i">$iter</span>+<span class="n">2</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>It prints (without so many line breaks)</p>
<pre class="verbatim"><ol><li>  [/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1]</li><li>			  [n 1]] 2]]] 1]</li><li>     [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]=0.198912</li><li>  pi=3.182598</li></ol></pre><p>The above module is very primitive.  It does not implement
mutator methods (<code class="inline">++</code>
, <code class="inline">-=</code>
 and so on), does not do deep copying
(not required without mutators!), and implements only those arithmetic
operations which are used in the example.</p>
<p>To implement most arithmetic operations is easy; one should just use
the tables of operations, and change the code which fills %subr to</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">%subr</span> = <span class="s">(</span> <span class="q">&#39;n&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">}</span> <span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$overload::ops</span>{<span class="w">with_assign</span>}<span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <span class="i">$subr</span>{<span class="q">&quot;$op=&quot;</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {shift() $op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">@bins</span> = <span class="q">qw(binary 3way_comparison num_comparison str_comparison)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="q">&quot;@overload::ops{ @bins }&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {shift() $op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="q">&quot;@overload::ops{qw(unary func)}&quot;</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;defining &#39;$op&#39;\n&quot;</span><span class="sc">;</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {$op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Since subroutines implementing assignment operators are not required
to modify their operands (see <a href="#Overloadable-Operations">Overloadable Operations</a> above),
we do not need anything special to make <code class="inline">+=</code>
 and friends work,
besides adding these operators to %subr and defining a copy
constructor (needed since Perl has no way to know that the
implementation of <code class="inline"><span class="q">&#39;+=&#39;</span></code>
 does not mutate the argument -
see <a href="#Copy-Constructor">Copy Constructor</a>).</p>
<p>To implement a copy constructor, add <code class="inline"><span class="q">&#39;=&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;cpy</span></code>
 to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>

line, and code (this code assumes that mutators change things one level
deep only, so recursive copying is not needed):</p>
<pre class="verbatim"><ol><li><a name="cpy"></a>  sub <span class="m">cpy</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="i">@$self</span><span class="s">]</span><span class="cm">,</span> <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$self</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>To make <code class="inline">++</code>
 and <code class="inline">--</code>
 work, we need to implement actual mutators,
either directly, or in <code class="inline"><span class="w">nomethod</span></code>
.  We continue to do things inside
<code class="inline"><span class="w">nomethod</span></code>
, thus add</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$meth</span> eq <span class="q">&#39;++&#39;</span> or <span class="i">$meth</span> eq <span class="q">&#39;--&#39;</span><span class="s">)</span> <span class="s">{</span></li><li>      <span class="i">@$obj</span> = <span class="s">(</span><span class="i">$meth</span><span class="cm">,</span> <span class="s">(</span><a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="i">@$obj</span><span class="s">]</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span> <span class="c"># Avoid circular reference</span></li><li>      <a class="l_k" href="functions/return.html">return</a> <span class="i">$obj</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>after the first line of wrap().  This is not a most effective
implementation, one may consider</p>
<pre class="verbatim"><ol><li><a name="inc"></a>  sub <span class="m">inc</span> <span class="s">{</span> <span class="i">$_</span>[<span class="n">0</span>] = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="q">&#39;++&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a><span class="cm">,</span> <span class="n">1</span><span class="s">]</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>instead.</p>
<p>As a final remark, note that one can fill %subr by</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">%subr</span> = <span class="s">(</span> <span class="q">&#39;n&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">}</span> <span class="s">)</span><span class="sc">;</span></li><li>  foreach <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$overload::ops</span>{<span class="w">with_assign</span>}<span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <span class="i">$subr</span>{<span class="q">&quot;$op=&quot;</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {shift() $op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">@bins</span> = <span class="q">qw(binary 3way_comparison num_comparison str_comparison)</span><span class="sc">;</span></li><li>  foreach <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="q">&quot;@overload::ops{ @bins }&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {shift() $op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  foreach <a class="l_k" href="functions/my.html">my</a> <span class="i">$op</span> <span class="s">(</span><a class="l_k" href="functions/split.html">split</a> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="q">&quot;@overload::ops{qw(unary func)}&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>    <span class="i">$subr</span>{<span class="i">$op</span>} = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&quot;sub {$op shift()}&quot;</span><span class="sc">;</span></li><li>  <span class="s">}</span></li><li>  <span class="i">$subr</span>{<span class="q">&#39;++&#39;</span>} = <span class="i">$subr</span>{<span class="q">&#39;+&#39;</span>}<span class="sc">;</span></li><li>  <span class="i">$subr</span>{<span class="q">&#39;--&#39;</span>} = <span class="i">$subr</span>{<span class="q">&#39;-&#39;</span>}<span class="sc">;</span></li></ol></pre><p>This finishes implementation of a primitive symbolic calculator in
50 lines of Perl code.  Since the numeric values of subexpressions
are not cached, the calculator is very slow.</p>
<p>Here is the answer for the exercise: In the case of str(), we need no
explicit recursion since the overloaded <code class="inline">.</code>-operator will fall back
to an existing overloaded operator <code class="inline"><span class="q">&quot;&quot;</span></code>
.  Overloaded arithmetic
operators <i>do not</i> fall back to numeric conversion if <code class="inline"><span class="w">fallback</span></code>
 is
not explicitly requested.  Thus without an explicit recursion num()
would convert <code class="inline"><span class="s">[</span><span class="q">&#39;+&#39;</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">]</span></code>
 to <code class="inline"><span class="i">$a</span> + <span class="i">$b</span></code>
, which would just rebuild
the argument of num().</p>
<p>If you wonder why defaults for conversion are different for str() and
num(), note how easy it was to write the symbolic calculator.  This
simplicity is due to an appropriate choice of defaults.  One extra
note: due to the explicit recursion num() is more fragile than sym():
we need to explicitly check for the type of $a and $b.  If components
$a and $b happen to be of some related type, this may lead to problems.</p>
<a name="_Really_-Symbolic-Calculator"></a><h2><i>Really</i> Symbolic Calculator</h2>
<p>One may wonder why we call the above calculator symbolic.  The reason
is that the actual calculation of the value of expression is postponed
until the value is <i>used</i>.</p>
<p>To see it in action, add a method</p>
<pre class="verbatim"><ol><li><a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$obj</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>    <span class="i">$#$obj</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="i">@$obj</span>-&gt;[<span class="n">0</span><span class="cm">,</span><span class="n">1</span>] = <span class="s">(</span><span class="q">&#39;=&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>to the package <code class="inline"><span class="w">symbolic</span></code>
.  After this change one can do</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = <span class="w">symbolic</span><span class="w">-&gt;new</span><span class="s">(</span><span class="n">4</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <a class="l_k" href="functions/sqrt.html">sqrt</a><span class="s">(</span><span class="i">$a</span>**<span class="n">2</span> + <span class="i">$b</span>**<span class="n">2</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>and the numeric value of $c becomes 5.  However, after calling</p>
<pre class="verbatim"><ol><li>  <span class="i">$a</span><span class="i">-&gt;STORE</span><span class="s">(</span><span class="n">12</span><span class="s">)</span><span class="sc">;</span>  <span class="i">$b</span><span class="i">-&gt;STORE</span><span class="s">(</span><span class="n">5</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>the numeric value of $c becomes 13.  There is no doubt now that the module
symbolic provides a <i>symbolic</i> calculator indeed.</p>
<p>To hide the rough edges under the hood, provide a tie()d interface to the
package <code class="inline"><span class="w">symbolic</span></code>
.  Add methods</p>
<pre class="verbatim"><ol><li><a name="TIESCALAR"></a>  sub <span class="m">TIESCALAR</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$pack</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <span class="i">$pack</span><span class="i">-&gt;new</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="s">}</span></li><li><a name="FETCH"></a>  sub <span class="m">FETCH</span> <span class="s">{</span> <a class="l_k" href="functions/shift.html">shift</a> <span class="s">}</span></li><li><a name="nop"></a>  sub <span class="m">nop</span> <span class="s">{</span>  <span class="s">}</span>		<span class="c"># Around a bug</span></li></ol></pre><p>(the bug, fixed in Perl 5.14, is described in <a href="#BUGS">BUGS</a>).  One can use this
new interface as</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$a</span><span class="cm">,</span> <span class="q">&#39;symbolic&#39;</span><span class="cm">,</span> <span class="n">3</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/tie.html">tie</a> <span class="i">$b</span><span class="cm">,</span> <span class="q">&#39;symbolic&#39;</span><span class="cm">,</span> <span class="n">4</span><span class="sc">;</span></li><li>  <span class="i">$a</span><span class="i">-&gt;nop</span><span class="sc">;</span>  <span class="i">$b</span><span class="i">-&gt;nop</span><span class="sc">;</span>	<span class="c"># Around a bug</span></li><li></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <a class="l_k" href="functions/sqrt.html">sqrt</a><span class="s">(</span><span class="i">$a</span>**<span class="n">2</span> + <span class="i">$b</span>**<span class="n">2</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Now numeric value of $c is 5.  After <code class="inline"><span class="i">$a</span> = <span class="n">12</span><span class="sc">;</span> <span class="i">$b</span> = <span class="n">5</span></code>
 the numeric value
of $c becomes 13.  To insulate the user of the module add a method</p>
<pre class="verbatim"><ol><li><a name="vars"></a>  sub <span class="m">vars</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$p</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/tie.html">tie</a><span class="s">(</span><span class="i">$_</span><span class="cm">,</span> <span class="i">$p</span><span class="s">)</span><span class="cm">,</span> <span class="i">$_</span><span class="i">-&gt;nop</span> foreach <span class="i">@_</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>Now</p>
<pre class="verbatim"><ol><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="sc">;</span></li><li>  <span class="w">symbolic</span><span class="w">-&gt;vars</span><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <a class="l_k" href="functions/sqrt.html">sqrt</a><span class="s">(</span><span class="i">$a</span>**<span class="n">2</span> + <span class="i">$b</span>**<span class="n">2</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>  <span class="i">$a</span> = <span class="n">3</span><span class="sc">;</span> <span class="i">$b</span> = <span class="n">4</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;c5  %s=%f\n&quot;</span><span class="cm">,</span> <span class="i">$c</span><span class="cm">,</span> <span class="i">$c</span><span class="sc">;</span></li><li></li><li>  <span class="i">$a</span> = <span class="n">12</span><span class="sc">;</span> <span class="i">$b</span> = <span class="n">5</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/printf.html">printf</a> <span class="q">&quot;c13  %s=%f\n&quot;</span><span class="cm">,</span> <span class="i">$c</span><span class="cm">,</span> <span class="i">$c</span><span class="sc">;</span></li></ol></pre><p>shows that the numeric value of $c follows changes to the values of $a
and $b.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Ilya Zakharevich &lt;<i>ilya@math.mps.ohio-state.edu</i>&gt;.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>The <code class="inline"><span class="w">overloading</span></code>
 pragma can be used to enable or disable overloaded
operations within a lexical scope - see <a href="overloading.html">overloading</a>.</p>
<a name="DIAGNOSTICS"></a><h1>DIAGNOSTICS</h1>
<p>When Perl is run with the <b>-Do</b> switch or its equivalent, overloading
induces diagnostic messages.</p>
<p>Using the <code class="inline"><a class="l_k" href="functions/m.html">m</a></code> command of Perl debugger (see <a href="perldebug.html">perldebug</a>) one can
deduce which operations are overloaded (and which ancestor triggers
this overloading).  Say, if <code class="inline">eq</code>
 is overloaded, then the method <code class="inline">(eq</code>
is shown by debugger.  The method <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 corresponds to the <code class="inline"><span class="w">fallback</span></code>

key (in fact a presence of this method shows that this package has
overloading enabled, and it is what is used by the <code class="inline"><span class="w">Overloaded</span></code>

function of module <code class="inline"><span class="w">overload</span></code>
).</p>
<p>The module might issue the following warnings:</p>
<ul>
<li><a name="Odd-number-of-arguments-for-overload%3a%3aconstant"></a><b>Odd number of arguments for overload::constant</b>
<p>(W) The call to overload::constant contained an odd number of arguments.
The arguments should come in pairs.</p>
</li>
<li><a name="'%25s'-is-not-an-overloadable-type"></a><b>'%s' is not an overloadable type</b>
<p>(W) You tried to overload a constant type the overload package is unaware of.</p>
</li>
<li><a name="'%25s'-is-not-a-code-reference"></a><b>'%s' is not a code reference</b>
<p>(W) The second (fourth, sixth, ...) argument of overload::constant needs
to be a code reference.  Either an anonymous subroutine, or a reference
to a subroutine.</p>
</li>
<li><a name="overload-arg-'%25s'-is-invalid"></a><b>overload arg '%s' is invalid</b>
<p>(W) <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 was passed an argument it did not
recognize.  Did you mistype an operator?</p>
</li>
</ul>
<a name="BUGS-AND-PITFALLS"></a><h1>BUGS AND PITFALLS</h1>
<ul>
<li>
<p>A pitfall when fallback is TRUE and Perl resorts to a built-in
implementation of an operator is that some operators have more
than one semantic, for example <code class="inline">|</code>:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;0+&#39;</span> <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$_</span>[<span class="n">0</span>]-&gt;{<span class="w">n</span>}<span class="sc">;</span> <span class="s">}</span><span class="cm">,</span></li><li>            <span class="w">fallback</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span> <span class="w">n</span> <span class="cm">=&gt;</span> <span class="n">4</span> <span class="s">}</span><span class="cm">,</span> <span class="q">&quot;main&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$y</span> = <a class="l_k" href="functions/bless.html">bless</a> <span class="s">{</span> <span class="w">n</span> <span class="cm">=&gt;</span> <span class="n">8</span> <span class="s">}</span><span class="cm">,</span> <span class="q">&quot;main&quot;</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span> | <span class="i">$y</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You might expect this to output "12".
In fact, it prints "&lt;": the ASCII result of treating "|"
as a bitwise string operator - that is, the result of treating
the operands as the strings "4" and "8" rather than numbers.
The fact that numify (<code class="inline"><span class="n">0</span>+</code>
) is implemented but stringify
(<code class="inline"><span class="q">&quot;&quot;</span></code>
) isn't makes no difference since the latter is simply
autogenerated from the former.</p>
<p>The only way to change this is to provide your own subroutine
for <code class="inline"><span class="q">&#39;|&#39;</span></code>
.</p>
</li>
<li>
<p>Magic autogeneration increases the potential for inadvertently
creating self-referential structures.
Currently Perl will not free self-referential
structures until cycles are explicitly broken.
For example,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> <span class="q">&#39;+&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;add&#39;</span><span class="sc">;</span></li><li><a name="add"></a>    sub <span class="m">add</span> <span class="s">{</span> <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span> \<span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> \<span class="i">$_</span>[<span class="n">1</span>] <span class="s">]</span> <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>is asking for trouble, since</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span> += <span class="i">$y</span><span class="sc">;</span></li></ol></pre><p>will effectively become</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span> = <span class="i">add</span><span class="s">(</span><span class="i">$obj</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>with the same result as</p>
<pre class="verbatim"><ol><li>    <span class="i">$obj</span> = <span class="s">[</span>\<span class="i">$obj</span><span class="cm">,</span> \<span class="i">$foo</span><span class="s">]</span><span class="sc">;</span></li></ol></pre><p>Even if no <i>explicit</i> assignment-variants of operators are present in
the script, they may be generated by the optimizer.
For example,</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;obj = $obj\n&quot;</span></li></ol></pre><p>may be optimized to</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$tmp</span> = <span class="q">&#39;obj = &#39;</span> . <span class="i">$obj</span><span class="sc">;</span>  <span class="i">$tmp</span> .= <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre></li>
<li>
<p>The symbol table is filled with names looking like line-noise.</p>
</li>
<li>
<p>This bug was fixed in Perl 5.18, but may still trip you up if you are using
older versions:</p>
<p>For the purpose of inheritance every overloaded package behaves as if
<code class="inline"><span class="w">fallback</span></code>
 is present (possibly undefined).  This may create
interesting effects if some package is not overloaded, but inherits
from two overloaded packages.</p>
</li>
<li>
<p>Before Perl 5.14, the relation between overloading and tie()ing was broken.
Overloading was triggered or not based on the <i>previous</i> class of the
tie()d variable.</p>
<p>This happened because the presence of overloading was checked
too early, before any tie()d access was attempted.  If the
class of the value FETCH()ed from the tied variable does not
change, a simple workaround for code that is to run on older Perl
versions is to access the value (via <code class="inline"><span class="s">(</span><span class="s">)</span> = <span class="i">$foo</span></code>
 or some such)
immediately after tie()ing, so that after this call the <i>previous</i> class
coincides with the current one.</p>
</li>
<li>
<p>Barewords are not covered by overloaded string constants.</p>
</li>
<li>
<p>The range operator <code class="inline">..</code>
 cannot be overloaded.</p>
</li>
</ul>




  <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><ul><li><a href="#Fundamentals">Fundamentals</a><li><a href="#Overloadable-Operations">Overloadable Operations</a><li><a href="#Magic-Autogeneration">Magic Autogeneration</a><li><a href="#Special-Keys-for-use-overload">Special Keys for use overload</a><li><a href="#How-Perl-Chooses-an-Operator-Implementation">How Perl Chooses an Operator Implementation</a><li><a href="#Losing-Overloading">Losing Overloading</a><li><a href="#Inheritance-and-Overloading">Inheritance and Overloading</a><li><a href="#Run-time-Overloading">Run-time Overloading</a><li><a href="#Public-Functions">Public Functions</a><li><a href="#Overloading-Constants">Overloading Constants</a></ul><li><a href="#IMPLEMENTATION">IMPLEMENTATION</a><li><a href="#COOKBOOK">COOKBOOK</a><ul><li><a href="#Two-face-Scalars">Two-face Scalars</a><li><a href="#Two-face-References">Two-face References</a><li><a href="#Symbolic-Calculator">Symbolic Calculator</a><li><a href="#_Really_-Symbolic-Calculator">_Really_ Symbolic Calculator</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#DIAGNOSTICS">DIAGNOSTICS</a><li><a href="#BUGS-AND-PITFALLS">BUGS AND PITFALLS</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    = 'overload';
  perldoc.pageAddress = 'overload.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>