File: overload.html

package info (click to toggle)
perl-doc-html 5.10.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 23,252 kB
  • ctags: 22,194
  • sloc: makefile: 6
file content (1275 lines) | stat: -rw-r--r-- 117,835 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<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 href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
</head>

<script language="JavaScript" type="text/javascript" src="label.js"></script>

<script language="JavaScript">
  pageDepth = 0;
  setPath();
</script>

<body onLoad="showToolbars();loadLabels()">

<div id="pageHeader">
  <div id="pageHeaderLogo">
    <img src="onion.gif">
  </div>
  <div id="pageHeaderText">
    <a href="http://perldoc.perl.org">perldoc.perl.org</a>
  </div>
</div>

<div id="pageBody">
  <div id="left">
    <div id="leftContent">
      <div id="leftClose">
        <a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
      </div>
      <h1>Manual:</h1>
      <ul>
        <li><a href="index-overview.html">Overview</a></li>
        <li><a href="index-tutorials.html">Tutorials</a></li>
        <li><a href="index-faq.html">FAQs</a></li>
        <li><a href="index-history.html">History / Changes</a></li>
        <li><a href="index-licence.html">Licence</a></li>
      </ul>
      <h2>Reference:</h2>
      <ul>
        <li><a href="index-language.html">Language</a></li>
        <li><a href="index-functions.html">Functions</a></li>
        <li><a href="perlop.html">Operators</a></li>
        <li><a href="perlvar.html">Special variables</a></li>
        <li><a href="index-pragmas.html">Pragmas</a></li>
        <li><a href="index-modules-A.html">Core modules</a></li>
        <li><a href="index-utilities.html">Utilities</a></li>
        <li><a href="index-internals.html">Internals</a></li>
        <li><a href="index-platforms.html">Platform specific</a></li>
      </ul>
      <h2>Links:</h2>
      <ul>
        <li><a href="http://search.cpan.org">CPAN</a></li>
        <li><a href="http://www.perl.org">Perl.org</a></li>
        <li><a href="http://www.perl.com">Perl.com</a></li>
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
        <li><a href="http://use.perl.org">Use Perl</a></li>
      </ul>
      <h2>Contact:</h2>
      <ul>
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
        <li class="spaced">Last updated on<br>23 December 2007</li>
	<li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
	more details</li>
      </ul>
    </div>
  </div>

  <div id="center">  
    <div id="centerContent">
      <div id="contentHeader">
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
      </div>
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-pragmas.html">Pragmas</a> &gt; overload</div>
      <script language="JavaScript">fromSearch();</script>
      <div id="contentBody"><div class="title_container"><div class="page_title">overload</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Declaration-of-overloaded-functions">Declaration of overloaded functions</a><li><a href="#Calling-Conventions-for-Binary-Operations">Calling Conventions for Binary Operations</a><li><a href="#Calling-Conventions-for-Unary-Operations">Calling Conventions for Unary Operations</a><li><a href="#Calling-Conventions-for-Mutators">Calling Conventions for Mutators</a><li><a href="#Overloadable-Operations">Overloadable Operations</a><li><a href="#Inheritance-and-overloading">Inheritance and overloading</a></ul><li><a href="#SPECIAL-SYMBOLS-FOR-'use-overload'">SPECIAL SYMBOLS FOR <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
</a><ul><li><a href="#Last-Resort">Last Resort</a><li><a href="#Fallback">Fallback</a><li><a href="#Smart-Match">Smart Match</a><li><a href="#Copy-Constructor">Copy Constructor</a></ul><li><a href="#MAGIC-AUTOGENERATION">MAGIC AUTOGENERATION</a><li><a href="#Minimal-set-of-overloaded-operations">Minimal set of overloaded operations</a><li><a href="#Losing-overloading">Losing 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><li><a href="#IMPLEMENTATION">IMPLEMENTATION</a><li><a href="#Metaphor-clash">Metaphor clash</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"><i>Really</i> symbolic calculator</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#DIAGNOSTICS">DIAGNOSTICS</a><li><a href="#BUGS">BUGS</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"><a name="package-SomeThing"></a>    package <span class="i">SomeThing</span><span class="sc">;</span></pre>
<pre class="verbatim">    <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;myadd</span><span class="cm">,</span>
	<span class="q">&#39;-&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;mysub</span><span class="sc">;</span>
	<span class="c"># etc</span>
    ...</pre>
<pre class="verbatim"><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span>
    <span class="i">$a</span> = <span class="w">new</span> <span class="w">SomeThing</span> <span class="n">57</span><span class="sc">;</span>
    <span class="i">$b</span>=<span class="n">5</span>+<span class="i">$a</span><span class="sc">;</span>
    ...
    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>
    ...
    <span class="i">$strval</span> = <span class="w">overload::StrVal</span> <span class="i">$b</span><span class="sc">;</span></pre>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<a name="Declaration-of-overloaded-functions"></a><h2>Declaration of overloaded functions</h2>
<p>The compilation directive</p>
<pre class="verbatim"><a name="package-Number"></a>    package <span class="i">Number</span><span class="sc">;</span>
    <a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span>
	<span class="q">&quot;+&quot;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;add</span><span class="cm">,</span>
	<span class="q">&quot;*=&quot;</span> <span class="cm">=&gt;</span> <span class="q">&quot;muas&quot;</span><span class="sc">;</span></pre>
<p>declares function Number::add() for addition, and method muas() in
the "class" <code class="inline"><span class="w">Number</span></code>
 (or one of its base classes)
for the assignment form <code class="inline"><span class="i">*=</span></code>
 of multiplication.</p>
<p>Arguments of this directive come in (key, value) pairs.  Legal values
are values legal inside a <code class="inline"><span class="i">&amp;</span>{ ... }</code>
 call, so the name of a
subroutine, a reference to a subroutine, or an anonymous subroutine
will all work.  Note that values specified as strings are
interpreted as methods, not subroutines.  Legal keys are listed below.</p>
<p>The subroutine <code class="inline"><span class="w">add</span></code>
 will be called to execute <code class="inline"><span class="i">$a</span>+<span class="i">$b</span></code>
 if $a
is a reference to an object blessed into the package <code class="inline"><span class="w">Number</span></code>
, or if $a is
not an object from a package with defined mathemagic addition, but $b is a
reference to a <code class="inline"><span class="w">Number</span></code>
.  It can also be called in other situations, like
<code class="inline"><span class="i">$a</span>+=<span class="n">7</span></code>
, or <code class="inline"><span class="i">$a</span>++</code>
.  See <a href="#MAGIC-AUTOGENERATION">"MAGIC AUTOGENERATION"</a>.  (Mathemagical
methods refer to methods triggered by an overloaded mathematical
operator.)</p>
<p>Since overloading respects inheritance via the @ISA hierarchy, the
above declaration would also trigger overloading of <code class="inline">+</code>
 and <code class="inline"><span class="i">*=</span></code>
 in
all the packages which inherit from <code class="inline"><span class="w">Number</span></code>
.</p>
<a name="Calling-Conventions-for-Binary-Operations"></a><h2>Calling Conventions for Binary Operations</h2>
<p>The functions specified in the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span> ...</code>
 directive are called
with three (in one particular case with four, see <a href="#Last-Resort">"Last Resort"</a>)
arguments.  If the corresponding operation is binary, then the first
two arguments are the two arguments of the operation.  However, due to
general object calling conventions, the first argument should always be
an object in the package, so in the situation of <code class="inline"><span class="n">7</span>+<span class="i">$a</span></code>
, the
order of the arguments is interchanged.  It probably does not matter
when implementing the addition method, but whether the arguments
are reversed is vital to the subtraction method.  The method can
query this information by examining the third argument, which can take
three different values:</p>
<ul>
<li><a name="FALSE"></a><b>FALSE</b>
<p>the order of arguments is as in the current operation.</p>
</li>
<li><a name="TRUE"></a><b>TRUE</b>
<p>the arguments are reversed.</p>
</li>
<li><a name="'undef'"></a><b><code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code></b>
<p>the current operation is an assignment variant (as in
<code class="inline"><span class="i">$a</span>+=<span class="n">7</span></code>
), but the usual function is called instead.  This additional
information can be used to generate some optimizations.  Compare
<a href="#Calling-Conventions-for-Mutators">"Calling Conventions for Mutators"</a>.</p>
</li>
</ul>
<a name="Calling-Conventions-for-Unary-Operations"></a><h2>Calling Conventions for Unary Operations</h2>
<p>Unary operation are considered binary operations with the second
argument being <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>.  Thus the functions that overloads <code class="inline"><span class="s">{</span><span class="q">&quot;++&quot;</span><span class="s">}</span></code>

is called with arguments <code class="inline"><span class="s">(</span><span class="i">$a</span><span class="cm">,</span><a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span><span class="q">&#39;&#39;</span><span class="s">)</span></code>
 when $a++ is executed.</p>
<a name="Calling-Conventions-for-Mutators"></a><h2>Calling Conventions for Mutators</h2>
<p>Two types of mutators have different calling conventions:</p>
<ul>
<li><a name="'%2b%2b'-and-'--'"></a><b><code class="inline">++</code>
 and <code class="inline">--</code>
</b>
<p>The routines which implement these operators are expected to actually
<i>mutate</i> their arguments.  So, assuming that $obj is a reference to a
number,</p>
<pre class="verbatim"><a name="incr"></a>  sub <span class="m">incr</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$n</span> = <span class="i">$</span> {<span class="i">$_</span>[<span class="n">0</span>]}<span class="sc">;</span> ++<span class="i">$n</span><span class="sc">;</span> <span class="i">$_</span>[<span class="n">0</span>] = <a class="l_k" href="functions/bless.html">bless</a> \<span class="i">$n</span><span class="s">}</span></pre>
<p>is an appropriate implementation of overloaded <code class="inline">++</code>
.  Note that</p>
<pre class="verbatim"><a name="incr"></a>  sub <span class="m">incr</span> <span class="s">{</span> ++<span class="i">$</span> {<span class="i">$_</span>[<span class="n">0</span>]} <span class="sc">;</span> <a class="l_k" href="functions/shift.html">shift</a> <span class="s">}</span></pre>
<p>is OK if used with preincrement and with postincrement. (In the case
of postincrement a copying will be performed, see <a href="#Copy-Constructor">"Copy Constructor"</a>.)</p>
</li>
<li><a name="'x%3d'-and-other-assignment-versions"></a><b><code class="inline"><span class="w">x</span>=</code>
 and other assignment versions</b>
<p>There is nothing special about these methods.  They may change the
value of their arguments, and may leave it as is.  The result is going
to be assigned to the value in the left-hand-side if different from
this value.</p>
<p>This allows for the same method to be used as overloaded <code class="inline">+=</code>
 and
<code class="inline">+</code>
.  Note that this is <i>allowed</i>, but not recommended, since by the
semantic of <a href="#Fallback">"Fallback"</a> Perl will call the method for <code class="inline">+</code>
 anyway,
if <code class="inline">+=</code>
 is not overloaded.</p>
</li>
</ul>
<p><b>Warning.</b>  Due to the presence of assignment versions of operations,
routines which may be called in assignment context may create
self-referential structures.  Currently Perl will not free self-referential
structures until cycles are <code class="inline"><span class="w">explicitly</span></code>
 broken.  You may get problems
when traversing your structures too.</p>
<p>Say,</p>
<pre class="verbatim">  <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="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></pre>
<p>is asking for trouble, since for code <code class="inline"><span class="i">$obj</span> += <span class="i">$foo</span></code>
 the subroutine
is called as <code class="inline"><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">$foo</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span></code>
, or <code class="inline"><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></code>
.  If using such a subroutine is an important optimization, one
can overload <code class="inline">+=</code>
 explicitly by a non-"optimized" version, or switch
to non-optimized version if <code class="inline">not <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$_</span>[<span class="n">2</span>]</code>
 (see
<a href="#Calling-Conventions-for-Binary-Operations">"Calling Conventions for Binary Operations"</a>).</p>
<p>Even if no <i>explicit</i> assignment-variants of operators are present in
the script, they may be generated by the optimizer.  Say, <code class="inline"><span class="q">&quot;,$obj,&quot;</span></code>
 or
<code class="inline"><span class="q">&#39;,&#39;</span> . <span class="i">$obj</span> . <span class="q">&#39;,&#39;</span></code>
 may be both optimized to</p>
<pre class="verbatim">  <a class="l_k" href="functions/my.html">my</a> <span class="i">$tmp</span> = <span class="q">&#39;,&#39;</span> . <span class="i">$obj</span><span class="sc">;</span>    <span class="i">$tmp</span> .= <span class="q">&#39;,&#39;</span><span class="sc">;</span></pre>
<a name="Overloadable-Operations"></a><h2>Overloadable Operations</h2>
<p>The following symbols can be specified in <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive:</p>
<ul>
<li><a name="*-_Arithmetic-operations_"></a><b><i>Arithmetic operations</i></b>
<pre class="verbatim">    <span class="q">&quot;+&quot;</span><span class="cm">,</span> <span class="q">&quot;+=&quot;</span><span class="cm">,</span> <span class="q">&quot;-&quot;</span><span class="cm">,</span> <span class="q">&quot;-=&quot;</span><span class="cm">,</span> <span class="q">&quot;*&quot;</span><span class="cm">,</span> <span class="q">&quot;*=&quot;</span><span class="cm">,</span> <span class="q">&quot;/&quot;</span><span class="cm">,</span> <span class="q">&quot;/=&quot;</span><span class="cm">,</span> <span class="q">&quot;%&quot;</span><span class="cm">,</span> <span class="q">&quot;%=&quot;</span><span class="cm">,</span>
    <span class="q">&quot;**&quot;</span><span class="cm">,</span> <span class="q">&quot;**=&quot;</span><span class="cm">,</span> <span class="q">&quot;&lt;&lt;&quot;</span><span class="cm">,</span> <span class="q">&quot;&lt;&lt;=&quot;</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;=&quot;</span><span class="cm">,</span> <span class="q">&quot;x&quot;</span><span class="cm">,</span> <span class="q">&quot;x=&quot;</span><span class="cm">,</span> <span class="q">&quot;.&quot;</span><span class="cm">,</span> <span class="q">&quot;.=&quot;</span><span class="cm">,</span></pre>
<p>For these operations a substituted non-assignment variant can be called if
the assignment variant is not available.  Methods for operations <code class="inline">+</code>
,
<code class="inline">-</code>
, <code class="inline">+=</code>
, and <code class="inline">-=</code>
 can be called to automatically generate
increment and decrement methods.  The operation <code class="inline">-</code>
 can be used to
autogenerate missing methods for unary minus or <code class="inline"><a class="l_k" href="functions/abs.html">abs</a></code>.</p>
<p>See <a href="#MAGIC-AUTOGENERATION">"MAGIC AUTOGENERATION"</a>, <a href="#Calling-Conventions-for-Mutators">"Calling Conventions for Mutators"</a> and
<a href="#Calling-Conventions-for-Binary-Operations">"Calling Conventions for Binary Operations"</a>) for details of these
substitutions.</p>
</li>
<li><a name="*-_Comparison-operations_"></a><b><i>Comparison operations</i></b>
<pre class="verbatim">    <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span>  <span class="q">&quot;&lt;=&quot;</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span>  <span class="q">&quot;&gt;=&quot;</span><span class="cm">,</span> <span class="q">&quot;==&quot;</span><span class="cm">,</span> <span class="q">&quot;!=&quot;</span><span class="cm">,</span> <span class="q">&quot;&lt;=&gt;&quot;</span><span class="cm">,</span>
    <span class="q">&quot;lt&quot;</span><span class="cm">,</span> <span class="q">&quot;le&quot;</span><span class="cm">,</span> <span class="q">&quot;gt&quot;</span><span class="cm">,</span> <span class="q">&quot;ge&quot;</span><span class="cm">,</span> <span class="q">&quot;eq&quot;</span><span class="cm">,</span> <span class="q">&quot;ne&quot;</span><span class="cm">,</span> <span class="q">&quot;cmp&quot;</span><span class="cm">,</span></pre>
<p>If the corresponding "spaceship" variant is available, it can be
used to substitute for the missing operation.  During <code class="inline"><a class="l_k" href="functions/sort.html">sort</a></code>ing
arrays, <code class="inline">cmp</code>
 is used to compare values subject to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
.</p>
</li>
<li><a name="*-_Bit-operations_"></a><b><i>Bit operations</i></b>
<pre class="verbatim">    <span class="q">&quot;&amp;&quot;</span><span class="cm">,</span> <span class="q">&quot;&amp;=&quot;</span><span class="cm">,</span> <span class="q">&quot;^&quot;</span><span class="cm">,</span> <span class="q">&quot;^=&quot;</span><span class="cm">,</span> <span class="q">&quot;|&quot;</span><span class="cm">,</span> <span class="q">&quot;|=&quot;</span><span class="cm">,</span> <span class="q">&quot;neg&quot;</span><span class="cm">,</span> <span class="q">&quot;!&quot;</span><span class="cm">,</span> <span class="q">&quot;~&quot;</span><span class="cm">,</span></pre>
<p><code class="inline"><span class="w">neg</span></code>
 stands for unary minus.  If the method for <code class="inline"><span class="w">neg</span></code>
 is not
specified, it can be autogenerated using the method for
subtraction. If the method for <code class="inline">!</code>
 is not specified, it can be
autogenerated using the methods for <code class="inline"><span class="w">bool</span></code>
, or <code class="inline"><span class="q">&quot;&quot;</span></code>
, or <code class="inline"><span class="n">0</span>+</code>
.</p>
<p>The same remarks in <a href="#Arithmetic-operations">"Arithmetic operations"</a> about
assignment-variants and autogeneration apply for
bit operations <code class="inline"><span class="q">&quot;&amp;&quot;</span></code>
, <code class="inline"><span class="q">&quot;^&quot;</span></code>
, and <code class="inline"><span class="q">&quot;|&quot;</span></code>
 as well.</p>
</li>
<li><a name="*-_Increment-and-decrement_"></a><b><i>Increment and decrement</i></b>
<pre class="verbatim">    <span class="q">&quot;++&quot;</span><span class="cm">,</span> <span class="q">&quot;--&quot;</span><span class="cm">,</span></pre>
<p>If undefined, addition and subtraction methods can be
used instead.  These operations are called both in prefix and
postfix form.</p>
</li>
<li><a name="*-_Transcendental-functions_"></a><b><i>Transcendental functions</i></b>
<pre class="verbatim">    <span class="q">&quot;atan2&quot;</span><span class="cm">,</span> <span class="q">&quot;cos&quot;</span><span class="cm">,</span> <span class="q">&quot;sin&quot;</span><span class="cm">,</span> <span class="q">&quot;exp&quot;</span><span class="cm">,</span> <span class="q">&quot;abs&quot;</span><span class="cm">,</span> <span class="q">&quot;log&quot;</span><span class="cm">,</span> <span class="q">&quot;sqrt&quot;</span><span class="cm">,</span> <span class="q">&quot;int&quot;</span></pre>
<p>If <code class="inline"><a class="l_k" href="functions/abs.html">abs</a></code> is unavailable, it can be autogenerated using methods
for "&lt;" or "&lt;=&gt;" combined with either unary minus or subtraction.</p>
<p>Note that traditionally the Perl function <a href="functions/int.html">int</a> rounds to 0, thus for
floating-point-like types one should follow the same semantic.  If
<code class="inline"><a class="l_k" href="functions/int.html">int</a></code> is unavailable, it can be autogenerated using the overloading of
<code class="inline"><span class="n">0</span>+</code>
.</p>
</li>
<li><a name="*-_Boolean%2c-string-and-numeric-conversion_"></a><b><i>Boolean, string and numeric conversion</i></b>
<pre class="verbatim">    <span class="q">&#39;bool&#39;</span><span class="cm">,</span> <span class="q">&#39;&quot;&quot;&#39;</span><span class="cm">,</span> <span class="q">&#39;0+&#39;</span><span class="cm">,</span></pre>
<p>If one or two of these operations are not overloaded, the remaining ones can
be used instead.  <code class="inline"><span class="w">bool</span></code>
 is used in the flow control operators
(like <code class="inline">while</code>
) and for the ternary <code class="inline">?:</code> operation.  These functions can
return any arbitrary Perl value.  If the corresponding operation for this value
is overloaded too, that 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>
</li>
<li><a name="*-_Iteration_"></a><b><i>Iteration</i></b>
<pre class="verbatim">    <span class="q">&quot;&lt;&gt;&quot;</span></pre>
<p>If not overloaded, the argument will be converted to a filehandle or
glob (which may require a stringification).  The same overloading
happens both for 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>
<p><b>BUGS</b> Even in list context, the iterator is currently called only
once and with scalar context.</p>
</li>
<li><a name="*-_Dereferencing_"></a><b><i>Dereferencing</i></b>
<pre class="verbatim">    <span class="q">&#39;${}&#39;</span><span class="cm">,</span> <span class="q">&#39;@{}&#39;</span><span class="cm">,</span> <span class="q">&#39;%{}&#39;</span><span class="cm">,</span> <span class="q">&#39;&amp;{}&#39;</span><span class="cm">,</span> <span class="q">&#39;*{}&#39;</span>.</pre>
<p>If not overloaded, the argument will be dereferenced <i>as is</i>, thus
should be of correct type.  These functions should return a reference
of correct type, or another object with overloaded dereferencing.</p>
<p>As a special case if the overload returns the object itself then it
will be used directly (provided it is the correct type).</p>
<p>The dereference operators must be specified explicitly they will not be passed to
"nomethod".</p>
</li>
<li><a name="*-_Special_"></a><b><i>Special</i></b>
<pre class="verbatim">    <span class="q">&quot;nomethod&quot;</span><span class="cm">,</span> <span class="q">&quot;fallback&quot;</span><span class="cm">,</span> <span class="q">&quot;=&quot;</span><span class="cm">,</span> <span class="q">&quot;~~&quot;</span><span class="cm">,</span></pre>
<p>see <a href="http://search.cpan.org/perldoc/SPECIAL SYMBOLS FOR <code class="inline"><a class="l_k" href="functions#use.html%22%3euse%3c%2fa%3e-%3cspan-class%3d%22w%22%3eoverload%3c%2fspan%3e%3c%2fcode%3e">"use.html">use</a> <span class="w">overload</span></code>" in SPECIAL SYMBOLS FOR <code class="inline"><a class="l_k" href="functions</a>.</p>
</li>
</ul>
<p>See <a href="#Fallback">"Fallback"</a> for an explanation of when a missing method can be
autogenerated.</p>
<p>A computer-readable form of the above table is available in the hash
%overload::ops, with values being space-separated lists of names:</p>
<pre class="verbatim"> <span class="w">with_assign</span>	  <span class="cm">=&gt;</span> <span class="q">'+ - * / % ** &lt;&lt; &gt;&gt; x .'</span><span class="cm">,</span>
 <span class="w">assign</span>		  <span class="cm">=&gt;</span> <span class="q">'+= -= *= /= %= **= &lt;&lt;= &gt;&gt;= x= .='</span><span class="cm">,</span>
 <span class="w">num_comparison</span>	  <span class="cm">=&gt;</span> <span class="q">'&lt; &lt;= &gt; &gt;= == !='</span><span class="cm">,</span>
 <span class="q">'3way_comparison'</span><span class="cm">=&gt;</span> <span class="q">'&lt;=&gt; cmp'</span><span class="cm">,</span>
 <span class="w">str_comparison</span>	  <span class="cm">=&gt;</span> <span class="q">'lt le gt ge eq ne'</span><span class="cm">,</span>
 <span class="w">binary</span>		  <span class="cm">=&gt;</span> <span class="q">'&amp; &amp;= | |= ^ ^='</span><span class="cm">,</span>
 <span class="w">unary</span>		  <span class="cm">=&gt;</span> <span class="q">'neg ! ~'</span><span class="cm">,</span>
 <span class="w">mutators</span>	  <span class="cm">=&gt;</span> <span class="q">'++ --'</span><span class="cm">,</span>
 <span class="w">func</span>		  <span class="cm">=&gt;</span> <span class="q">'atan2 cos sin exp abs log sqrt'</span><span class="cm">,</span>
 <span class="w">conversion</span>	  <span class="cm">=&gt;</span> <span class="q">'bool &quot;&quot; 0+'</span><span class="cm">,</span>
 <span class="w">iterators</span>	  <span class="cm">=&gt;</span> <span class="q">'&lt;&gt;'</span><span class="cm">,</span>
 <span class="w">dereferencing</span>	  <span class="cm">=&gt;</span> <span class="q">'${} @{} %{} &amp;{} *{}'</span><span class="cm">,</span>
 <span class="w">special</span>	  <span class="cm">=&gt;</span> <span class="q">'nomethod fallback ='</span></pre>
<a name="Inheritance-and-overloading"></a><h2>Inheritance and overloading</h2>
<p>Inheritance interacts with overloading in two ways.</p>
<ul>
<li><a name="Strings-as-values-of-'use-overload'-directive"></a><b>Strings as values of <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">  <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></pre>
<p>is a string, it is interpreted as a method name.</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.  The
set of overloaded methods is the union of overloaded methods of all
the ancestors. If some method is overloaded in several ancestor, then
which description will be used is decided by the usual inheritance
rules:</p>
<p>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 this 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 since the value of the <code class="inline"><span class="w">fallback</span></code>
 key is not a subroutine,
its inheritance is not governed by the above rules.  In the current
implementation, the value of <code class="inline"><span class="w">fallback</span></code>
 in the first overloaded
ancestor is used, but this is accidental and subject to change.</p>
<a name="SPECIAL-SYMBOLS-FOR-'use-overload'"></a><h1>SPECIAL SYMBOLS FOR <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
</h1>
<p>Three keys are recognized by Perl that are not covered by the above
description.</p>
<a name="Last-Resort"></a><h2>Last Resort</h2>
<p><code class="inline"><span class="q">&quot;nomethod&quot;</span></code>
 should be followed by a reference to a function of four
parameters.  If defined, it is called when the overloading mechanism
cannot find a method for some operation.  The first three arguments of
this function coincide with the arguments for the corresponding method if
it were found, the fourth argument is the symbol
corresponding to the missing method.  If several methods are tried,
the last one is used.  Say, <code class="inline"><span class="n">1</span>-<span class="i">$a</span></code>
 can be equivalent to</p>
<pre class="verbatim">	<span class="i">&amp;nomethodMethod</span><span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="q">&quot;-&quot;</span><span class="s">)</span></pre>
<p>if the pair <code class="inline"><span class="q">&quot;nomethod&quot;</span> <span class="cm">=&gt;</span> <span class="q">&quot;nomethodMethod&quot;</span></code>
 was specified in the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive.</p>
<p>The <code class="inline"><span class="q">&quot;nomethod&quot;</span></code>
 mechanism is <i>not</i> used for the dereference operators
( ${} @{} %{} &amp;{} *{} ).</p>
<p>If some operation cannot be resolved, and there is no function
assigned to <code class="inline"><span class="q">&quot;nomethod&quot;</span></code>
, then an exception will be raised via die()--
unless <code class="inline"><span class="q">&quot;fallback&quot;</span></code>
 was specified as a key in <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 directive.</p>
<a name="Fallback"></a><h2>Fallback</h2>
<p>The key <code class="inline"><span class="q">&quot;fallback&quot;</span></code>
 governs what to do if a method for a particular
operation is not found.  Three different cases are possible depending on
the value of <code class="inline"><span class="q">&quot;fallback&quot;</span></code>
:</p>
<ul>
<li><a name="*-'undef'"></a><b><code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code></b>
<p>Perl tries to use a
substituted method (see <a href="#MAGIC-AUTOGENERATION">"MAGIC AUTOGENERATION"</a>).  If this fails, it
then tries to calls <code class="inline"><span class="q">&quot;nomethod&quot;</span></code>
 value; if missing, an exception
will be raised.</p>
</li>
<li><a name="*-TRUE"></a><b>TRUE</b>
<p>The same as for the <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> value, but no exception is raised.  Instead,
it silently reverts to what it would have done were there no <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>

present.</p>
</li>
<li><a name="*-defined%2c-but-FALSE"></a><b>defined, but FALSE</b>
<p>No autogeneration is tried.  Perl tries to call
<code class="inline"><span class="q">&quot;nomethod&quot;</span></code>
 value, and if this is missing, raises an exception.</p>
</li>
</ul>
<p><b>Note.</b> <code class="inline"><span class="q">&quot;fallback&quot;</span></code>
 inheritance via @ISA is not carved in stone
yet, see <a href="#Inheritance-and-overloading">"Inheritance and overloading"</a>.</p>
<a name="Smart-Match"></a><h2>Smart Match</h2>
<p>The key <code class="inline"><span class="q">&quot;~~&quot;</span></code>
 allows you to override the smart matching used by
the switch construct. See <a href="feature.html">feature</a>.</p>
<a name="Copy-Constructor"></a><h2>Copy Constructor</h2>
<p>The value for <code class="inline"><span class="q">&quot;=&quot;</span></code>
 is a reference to a function with three
arguments, i.e., it looks like the other values in <code class="inline"><a class="l_k" href="functions/use.html">use</a>
<span class="w">overload</span></code>
. However, it does not overload the Perl assignment
operator. This would go against Camel hair.</p>
<p>This operation is called in the situations when a mutator is applied
to a reference that shares its object with some other reference, such
as</p>
<pre class="verbatim">	<span class="i">$a</span>=<span class="i">$b</span><span class="sc">;</span>
	++<span class="i">$a</span><span class="sc">;</span></pre>
<p>To make this change $a and not change $b, a copy of <code class="inline"><span class="i">$$a</span></code>
 is made,
and $a is assigned a reference to this new object.  This operation is
done during execution of the <code class="inline">++<span class="i">$a</span></code>
, and not during the assignment,
(so before the increment <code class="inline"><span class="i">$$a</span></code>
 coincides with <code class="inline"><span class="i">$$b</span></code>
).  This is only
done if <code class="inline">++</code>
 is expressed via a method for <code class="inline"><span class="q">&#39;++&#39;</span></code>
 or <code class="inline"><span class="q">&#39;+=&#39;</span></code>
 (or
<code class="inline"><span class="w">nomethod</span></code>
).  Note that if this operation is expressed via <code class="inline"><span class="q">&#39;+&#39;</span></code>

a nonmutator, i.e., as in</p>
<pre class="verbatim">	<span class="i">$a</span>=<span class="i">$b</span><span class="sc">;</span>
	<span class="i">$a</span>=<span class="i">$a</span>+<span class="n">1</span><span class="sc">;</span></pre>
<p>then <code class="inline"><span class="i">$a</span></code>
 does not reference a new copy of <code class="inline"><span class="i">$$a</span></code>
, since $$a does not
appear as lvalue when the above code is executed.</p>
<p>If the copy constructor is required during the execution of some mutator,
but a method for <code class="inline"><span class="q">&#39;=&#39;</span></code>
 was not specified, it can be autogenerated as a
string copy if the object is a plain scalar.</p>
<ul>
<li><a name="*Example*"></a><b><b>Example</b></b>
<p>The actually executed code for</p>
<pre class="verbatim">	$a=$b;
        Something else which does not modify $a or $b....
	++$a;</pre><p>may be</p>
<pre class="verbatim">	$a=$b;
        Something else which does not modify $a or $b....
	$a = $a-&gt;clone(undef,"");
        $a-&gt;incr(undef,"");</pre><p>if $b was mathemagical, and <code class="inline"><span class="q">&#39;++&#39;</span></code>
 was overloaded with <code class="inline">\<span class="i">&amp;incr</span></code>
,
<code class="inline"><span class="q">&#39;=&#39;</span></code>
 was overloaded with <code class="inline">\<span class="i">&amp;clone</span></code>
.</p>
</li>
</ul>
<p>Same behaviour is triggered by <code class="inline"><span class="i">$b</span> = <span class="i">$a</span>++</code>
, which is consider a synonym for
<code class="inline"><span class="i">$b</span> = <span class="i">$a</span><span class="sc">;</span> ++<span class="i">$a</span></code>
.</p>
<a name="MAGIC-AUTOGENERATION"></a><h1>MAGIC AUTOGENERATION</h1>
<p>If a method for an operation is not found, and the value for  <code class="inline"><span class="q">&quot;fallback&quot;</span></code>
 is
TRUE or undefined, Perl tries to autogenerate a substitute method for
the missing operation based on the defined operations.  Autogenerated method
substitutions are possible for the following operations:</p>
<ul>
<li><a name="_Assignment-forms-of-arithmetic-operations_"></a><b><i>Assignment forms of arithmetic operations</i></b>
<p><code class="inline"><span class="i">$a</span>+=<span class="i">$b</span></code>
 can use the method for <code class="inline"><span class="q">&quot;+&quot;</span></code>
 if the method for <code class="inline"><span class="q">&quot;+=&quot;</span></code>

is not defined.</p>
</li>
<li><a name="_Conversion-operations_"></a><b><i>Conversion operations</i></b>
<p>String, numeric, and boolean conversion are calculated in terms of one
another if not all of them are defined.</p>
</li>
<li><a name="_Increment-and-decrement_"></a><b><i>Increment and decrement</i></b>
<p>The <code class="inline">++<span class="i">$a</span></code>
 operation can be expressed in terms of <code class="inline"><span class="i">$a</span>+=<span class="n">1</span></code>
 or <code class="inline"><span class="i">$a</span>+<span class="n">1</span></code>
,
and <code class="inline"><span class="i">$a</span>--</code>
 in terms of <code class="inline"><span class="i">$a</span>-=<span class="n">1</span></code>
 and <code class="inline"><span class="i">$a</span>-<span class="n">1</span></code>
.</p>
</li>
<li><a name="'abs(%24a)'"></a><b><code class="inline"><a class="l_k" href="functions/abs.html">abs($a)</a></code></b>
<p>can be expressed in terms of <code class="inline"><span class="i">$a</span>&lt;<span class="n">0</span></code>
 and <code class="inline">-<span class="i">$a</span></code>
 (or <code class="inline"><span class="n">0</span>-<span class="i">$a</span></code>
).</p>
</li>
<li><a name="_Unary-minus_"></a><b><i>Unary minus</i></b>
<p>can be expressed in terms of subtraction.</p>
</li>
<li><a name="_Negation_"></a><b><i>Negation</i></b>
<p><code class="inline">!</code>
 and <code class="inline">not</code>
 can be expressed in terms of boolean conversion, or
string or numerical conversion.</p>
</li>
<li><a name="_Concatenation_"></a><b><i>Concatenation</i></b>
<p>can be expressed in terms of string conversion.</p>
</li>
<li><a name="_Comparison-operations_"></a><b><i>Comparison operations</i></b>
<p>can be expressed in terms of its "spaceship" counterpart: either
<code class="inline">&lt;=&gt;</code>
 or <code class="inline">cmp</code>
:</p>
<pre class="verbatim">    <span class="q">&lt;, &gt;</span><span class="cm">,</span> &lt;=<span class="cm">,</span> &gt;=<span class="cm">,</span> ==<span class="cm">,</span> != 	<span class="w">in</span> <span class="w">terms</span> <span class="w">of</span> &lt;=&gt;
    lt<span class="cm">,</span> gt<span class="cm">,</span> le<span class="cm">,</span> ge<span class="cm">,</span> eq<span class="cm">,</span> ne 	<span class="w">in</span> <span class="w">terms</span> <span class="w">of</span> cmp</pre>
</li>
<li><a name="_Iterator_"></a><b><i>Iterator</i></b>
<pre class="verbatim">    &lt;&gt;				in terms of builtin operations</pre></li>
<li><a name="_Dereferencing_"></a><b><i>Dereferencing</i></b>
<pre class="verbatim">    ${} @{} %{} &amp;{} *{}		in terms of builtin operations</pre></li>
<li><a name="_Copy-operator_"></a><b><i>Copy operator</i></b>
<p>can be expressed in terms of an assignment to the dereferenced value, if this
value is a scalar and not a reference.</p>
</li>
</ul>
<a name="Minimal-set-of-overloaded-operations"></a><h1>Minimal set of overloaded operations</h1>
<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. See <a href="#MAGIC-AUTOGENERATION">"MAGIC AUTOGENERATION"</a> above. The minimal set is:</p>
<pre class="verbatim">    + - <span class="i">* /</span> % <span class="i">**</span> &lt;&lt; &gt;&gt; <span class="w">x</span>
    &lt;=&gt; cmp
    <span class="i">&amp; |</span> ^ ~
    <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></pre>
<p>Additionally, you need to define at least one of string, boolean or
numeric conversions because any one can be used to emulate the others.
The string conversion can also be used to emulate concatenation.</p>
<a name="Losing-overloading"></a><h1>Losing overloading</h1>
<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="Run-time-Overloading"></a><h1>Run-time Overloading</h1>
<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">    <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></pre>
<p>You can also use</p>
<pre class="verbatim">    <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></pre>
<p>though the use of these constructs during run-time is questionable.</p>
<a name="Public-functions"></a><h1>Public functions</h1>
<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 string value of <code class="inline"><span class="w">arg</span></code>
 as in 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><h1>Overloading constants</h1>
<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"><a name="import"></a>	sub <span class="m">import</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/return.html">return</a> unless <span class="i">@_</span><span class="sc">;</span>
	  <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;unknown import: @_&quot;</span> unless <span class="i">@_</span> == <span class="n">1</span> and <span class="i">$_</span>[<span class="n">0</span>] eq <span class="q">&#39;:constant&#39;</span><span class="sc">;</span>
	  <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>
	<span class="s">}</span></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. However, this invalidation remains
unprocessed until the next <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code>ing into the package. Hence if you
want to change overloading structure dynamically, you'll need an
additional (fake) <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code>ing to update the table.</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 an object belongs to a package using overload, it carries a special
flag.  Thus the only speed penalty during arithmetic operations without
overloading is the checking of this flag.</p>
<p>In fact, if <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 is not present, there is almost no overhead
for overloadable operations, so most programs should not suffer
measurable performance penalties.  A considerable effort was made to
minimize the overhead when overload is used in some package, but the
arguments in question do not belong to packages using overload.  When
in doubt, test your speed with <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 and without it.  So far
there have been no reports of substantial speed degradation if Perl is
compiled with optimization turned on.</p>
<p>There is no size penalty for data if overload is not used. The only
size penalty if overload is used in some package is that <i>all</i> the
packages acquire a magic during the next <code class="inline"><a class="l_k" href="functions/bless.html">bless</a></code>ing into the
package. This magic is three-words-long for packages without
overloading, and carries the cache table if the package is overloaded.</p>
<p>Copying (<code class="inline"><span class="i">$a</span>=<span class="i">$b</span></code>
) is shallow; however, a one-level-deep copying is
carried out before any operation that can imply an assignment to the
object $a (or $b) refers to, like <code class="inline"><span class="i">$a</span>++</code>
.  You can override this
behavior by defining your own copy constructor (see <a href="#Copy-Constructor">"Copy Constructor"</a>).</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="Metaphor-clash"></a><h1>Metaphor clash</h1>
<p>One may wonder why the semantic of overloaded <code class="inline">=</code>
 is so counter intuitive.
If it <i>looks</i> counter intuitive to you, you are subject to a metaphor
clash.</p>
<p>Here is a Perl object metaphor:</p>
<p><i>  object is a reference to blessed data</i></p>
<p>and an arithmetic metaphor:</p>
<p><i>  object is a thing by itself</i>.</p>
<p>The <i>main</i> problem of overloading <code class="inline">=</code>
 is the fact that these metaphors
imply different actions on the assignment <code class="inline"><span class="i">$a</span> = <span class="i">$b</span></code>
 if $a and $b are
objects.  Perl-think implies that $a becomes a reference to whatever
$b was referencing.  Arithmetic-think implies that the value of "object"
$a is changed to become the value of the object $b, preserving the fact
that $a and $b are separate entities.</p>
<p>The difference is not relevant in the absence of mutators.  After
a Perl-way assignment an operation which mutates the data referenced by $a
would change the data referenced by $b too.  Effectively, after
<code class="inline"><span class="i">$a</span> = <span class="i">$b</span></code>
 values of $a and $b become <i>indistinguishable</i>.</p>
<p>On the other hand, anyone who has used algebraic notation knows the
expressive power of the arithmetic metaphor.  Overloading works hard
to enable this metaphor while preserving the Perlian way as far as
possible.  Since it is not possible to freely mix two contradicting
metaphors, overloading allows the arithmetic way to write things <i>as
far as all the mutators are called via overloaded access only</i>.  The
way it is done is described in <a href="#Copy-Constructor">"Copy Constructor"</a>.</p>
<p>If some mutator methods are directly applied to the overloaded values,
one may need to <i>explicitly unlink</i> other values which references the
same value:</p>
<pre class="verbatim">    <span class="i">$a</span> = <span class="w">new</span> <span class="w">Data</span> <span class="n">23</span><span class="sc">;</span>
    ...
    <span class="i">$b</span> = <span class="i">$a</span><span class="sc">;</span>		<span class="c"># $b is &quot;linked&quot; to $a</span>
    ...
    <span class="i">$a</span> = <span class="i">$a</span><span class="i">-&gt;clone</span><span class="sc">;</span>	<span class="c"># Unlink $b from $a</span>
    <span class="i">$a</span><span class="i">-&gt;increment_by</span><span class="s">(</span><span class="n">4</span><span class="s">)</span><span class="sc">;</span></pre>
<p>Note that overloaded access makes this transparent:</p>
<pre class="verbatim">    <span class="i">$a</span> = <span class="w">new</span> <span class="w">Data</span> <span class="n">23</span><span class="sc">;</span>
    <span class="i">$b</span> = <span class="i">$a</span><span class="sc">;</span>		<span class="c"># $b is &quot;linked&quot; to $a</span>
    <span class="i">$a</span> += <span class="n">4</span><span class="sc">;</span>		<span class="c"># would unlink $b automagically</span></pre>
<p>However, it would not make</p>
<pre class="verbatim">    <span class="i">$a</span> = <span class="w">new</span> <span class="w">Data</span> <span class="n">23</span><span class="sc">;</span>
    <span class="i">$a</span> = <span class="n">4</span><span class="sc">;</span>		<span class="c"># Now $a is a plain 4, not &#39;Data&#39;</span></pre>
<p>preserve "objectness" of $a.  But Perl <i>has</i> a way to make assignments
to an object do whatever you want.  It is just not the overload, but
tie()ing interface (see <a href="functions/tie.html">tie</a>).  Adding a FETCH() method
which returns the object itself, and STORE() method which changes the
value of the object, one can reproduce the arithmetic metaphor in its
completeness, at least for variables which were tie()d from the start.</p>
<p>(Note that a workaround for a bug may be needed, see <a href="#BUGS">"BUGS"</a>.)</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"><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>
                                <span class="c"># numeric values.</span>
<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>
  <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>
<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>
<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></pre>
<p>Use it as follows:</p>
<pre class="verbatim">  <a class="l_k" href="functions/require.html">require</a> <span class="w">two_face</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$seven</span> = <span class="w">new</span> <span class="w">two_face</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>
  <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>
  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;seven contains `i&#39;\n&quot;</span> if <span class="i">$seven</span> =~ <span class="q">/i/</span><span class="sc">;</span></pre>
<p>(The second line creates a scalar which has both a string value, and a
numeric value.)  This prints:</p>
<pre class="verbatim">  seven=vii, seven=7, eight=8
  seven contains `i'</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"><a name="package-two_refs"></a>  package <span class="i">two_refs</span><span class="sc">;</span>
  <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>
<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="sc">;</span>
  <span class="s">}</span>
<a name="gethash"></a>  sub <span class="m">gethash</span> <span class="s">{</span>
    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span>
    <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>
    <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>
    \<span class="i">%h</span><span class="sc">;</span>
  <span class="s">}</span></pre>
<pre class="verbatim"><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>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">%fields</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span>
  <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>
<a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <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>
  <span class="s">}</span>
<a name="FETCH"></a>  sub <span class="m">FETCH</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <span class="i">$$self</span>-&gt;[<span class="i">$key</span>]<span class="sc">;</span>
  <span class="s">}</span></pre>
<p>Now one can access an object using both the array and hash syntax:</p>
<pre class="verbatim">  <a class="l_k" href="functions/my.html">my</a> <span class="i">$bar</span> = <span class="w">new</span> <span class="w">two_refs</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="sc">;</span>
  <span class="i">$bar</span>-&gt;[<span class="n">2</span>] = <span class="n">11</span><span class="sc">;</span>
  <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></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"><a name="access_hash"></a>  sub <span class="m">access_hash</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <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>
    <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>
    <span class="i">$out</span><span class="sc">;</span>
  <span class="s">}</span></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"><a name="package-two_refs1"></a>  package <span class="i">two_refs1</span><span class="sc">;</span>
  <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>
               <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>
<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/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>
    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span>
    <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>
    <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>
  <span class="s">}</span>
<a name="gethash"></a>  sub <span class="m">gethash</span> <span class="s">{</span>
    <a class="l_k" href="functions/my.html">my</a> <span class="i">%h</span><span class="sc">;</span>
    <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>
    <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>
    \<span class="i">%h</span><span class="sc">;</span>
  <span class="s">}</span></pre>
<pre class="verbatim"><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>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">%fields</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i</span> = <span class="n">0</span><span class="sc">;</span>
  <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>
<a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <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>
  <span class="s">}</span>
<a name="FETCH"></a>  sub <span class="m">FETCH</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <span class="i">$a</span>-&gt;[<span class="i">$key</span>]<span class="sc">;</span>
  <span class="s">}</span></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"><a name="package-symbolic"></a>  package <span class="i">symbolic</span><span class="sc">;</span>		<span class="c"># Primitive symbolic calculator</span>
  <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></pre>
<pre class="verbatim"><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>
<a name="wrap"></a>  sub <span class="m">wrap</span> <span class="s">{</span>
    <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>
    <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>
    <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>
  <span class="s">}</span></pre>
<p>This module is very unusual as overloaded modules go: it does not
provide any usual overloaded operators, instead it provides the <a href="#Last-Resort">"Last Resort"</a> operator <code class="inline"><span class="w">nomethod</span></code>
.  In this example the corresponding
subroutine returns an object which encapsulates operations done over
the objects: <code class="inline"><span class="w">new</span> <span class="w">symbolic</span> <span class="n">3</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">new</span>
<span class="w">symbolic</span> <span class="n">3</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">  <a class="l_k" href="functions/require.html">require</a> <span class="w">symbolic</span><span class="sc">;</span>
  <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>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$side</span> = <span class="w">new</span> <span class="w">symbolic</span> <span class="n">1</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$cnt</span> = <span class="i">$iter</span><span class="sc">;</span></pre>
<pre class="verbatim">  while <span class="s">(</span><span class="i">$cnt</span>--<span class="s">)</span> <span class="s">{</span>
    <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>
  <span class="s">}</span>
  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;OK\n&quot;</span><span class="sc">;</span></pre>
<p>The value of $side is</p>
<pre class="verbatim">  <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>
	               <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></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"><a name="pretty"></a>  sub <span class="m">pretty</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <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>
    <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>
    <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span>
  <span class="s">}</span></pre>
<p>Now one can finish the script by</p>
<pre class="verbatim">  <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></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">  <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>
<a name="str"></a>  sub <span class="m">str</span> <span class="s">{</span>
    <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>
    <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>
    <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>
    <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span>
  <span class="s">}</span></pre>
<p>Now one can change the last line of the script to</p>
<pre class="verbatim">  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;side = $side\n&quot;</span><span class="sc">;</span></pre>
<p>which outputs</p>
<pre class="verbatim">  side = [/ [- [sqrt [+ 1 [** [n 1 u] 2]] u] 1] [n 1 u]]</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"><a name="package-symbolic"></a>  package <span class="i">symbolic</span><span class="sc">;</span>		<span class="c"># Primitive symbolic calculator</span>
  <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="cm">,</span> <span class="q">&#39;0+&#39;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;num</span><span class="sc">;</span></pre>
<pre class="verbatim"><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>
<a name="wrap"></a>  sub <span class="m">wrap</span> <span class="s">{</span>
    <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>
    <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>
    <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>
  <span class="s">}</span>
<a name="str"></a>  sub <span class="m">str</span> <span class="s">{</span>
    <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>
    <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>
    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>
      <span class="q">&quot;[$meth $a $b]&quot;</span><span class="sc">;</span>
    <span class="s">}</span> else <span class="s">{</span>
      <span class="q">&quot;[$meth $a]&quot;</span><span class="sc">;</span>
    <span class="s">}</span>
  <span class="s">}</span>
  <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>
	       <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>
	       <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>
	       <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>
	       <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>
	       <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>
	       <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>
	     <span class="s">)</span><span class="sc">;</span>
<a name="num"></a>  sub <span class="m">num</span> <span class="s">{</span>
    <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>
    <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>}
      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>
    <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>
    <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>
    <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>
  <span class="s">}</span></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">  <a class="l_k" href="functions/require.html">require</a> <span class="w">symbolic</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$iter</span> = <span class="w">new</span> <span class="w">symbolic</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># 16-gon</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$side</span> = <span class="w">new</span> <span class="w">symbolic</span> <span class="n">1</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$cnt</span> = <span class="i">$iter</span><span class="sc">;</span></pre>
<pre class="verbatim">  while <span class="s">(</span><span class="i">$cnt</span><span class="s">)</span> <span class="s">{</span>
    <span class="i">$cnt</span> = <span class="i">$cnt</span> - <span class="n">1</span><span class="sc">;</span>		<span class="c"># Mutator `--&#39; not implemented</span>
    <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>
  <span class="s">}</span>
  <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>
  <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></pre>
<p>It prints (without so many line breaks)</p>
<pre class="verbatim">  [/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1]
			  [n 1]] 2]]] 1]
     [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]=0.198912
  pi=3.182598</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">  <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>
  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>
    <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>
  <span class="s">}</span>
  <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>
  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>
    <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>
  <span class="s">}</span>
  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>
    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;defining `$op&#39;\n&quot;</span><span class="sc">;</span>
    <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>
  <span class="s">}</span></pre>
<p>Due to <a href="#Calling-Conventions-for-Mutators">"Calling Conventions for Mutators"</a>, we do not need anything
special to make <code class="inline">+=</code>
 and friends work, except filling <code class="inline">+=</code>
 entry of
%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, compare <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"><a name="cpy"></a>  sub <span class="m">cpy</span> <span class="s">{</span>
    <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>
    <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>
  <span class="s">}</span></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">    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>
      <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>
      <a class="l_k" href="functions/return.html">return</a> <span class="i">$obj</span><span class="sc">;</span>
    <span class="s">}</span></pre>
<p>after the first line of wrap().  This is not a most effective
implementation, one may consider</p>
<pre class="verbatim"><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></pre>
<p>instead.</p>
<p>As a final remark, note that one can fill %subr by</p>
<pre class="verbatim">  <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>
  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>
    <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>
  <span class="s">}</span>
  <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>
  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>
    <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>
  <span class="s">}</span>
  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>
    <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>
  <span class="s">}</span>
  <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>
  <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></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"><a name="STORE"></a>  sub <span class="m">STORE</span> <span class="s">{</span>
    <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>
    <span class="i">$#$obj</span> = <span class="n">1</span><span class="sc">;</span>
    <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>
  <span class="s">}</span></pre>
<p>to the package <code class="inline"><span class="w">symbolic</span></code>
.  After this change one can do</p>
<pre class="verbatim">  <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="w">new</span> <span class="w">symbolic</span> <span class="n">3</span><span class="sc">;</span>
  <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = <span class="w">new</span> <span class="w">symbolic</span> <span class="n">4</span><span class="sc">;</span>
  <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></pre>
<p>and the numeric value of $c becomes 5.  However, after calling</p>
<pre class="verbatim">  <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></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>
 (compare with <a href="#Metaphor-clash">"Metaphor clash"</a>).  Add methods</p>
<pre class="verbatim"><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>
<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>
<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></pre>
<p>(the bug is described in <a href="#BUGS">"BUGS"</a>).  One can use this new interface as</p>
<pre class="verbatim">  <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>
  <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>
  <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></pre>
<pre class="verbatim">  <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></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"><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></pre>
<p>Now</p>
<pre class="verbatim">  <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>
  <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>
  <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></pre>
<pre class="verbatim">  <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>
  <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></pre>
<pre class="verbatim">  <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>
  <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></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="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="%60%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="%60%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>
</ul>
<a name="BUGS"></a><h1>BUGS</h1>
<p>Because it is used for overloading, the per-package hash %OVERLOAD now
has a special meaning in Perl. The symbol table is filled with names
looking like line-noise.</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>
<p>Relation between overloading and tie()ing is broken.  Overloading is
triggered or not basing on the <i>previous</i> class of tie()d value.</p>
<p>This happens because the presence of overloading is checked too early,
before any tie()d access is attempted.  If the FETCH()ed class of the
tie()d value does not change, a simple workaround is to access the value
immediately after tie()ing, so that after this call the <i>previous</i> class
coincides with the current one.</p>
<p><b>Needed:</b> a way to fix this without a speed penalty.</p>
<p>Barewords are not covered by overloaded string constants.</p>
<p>This document is confusing.  There are grammos and misleading language
used in places.  It would seem a total rewrite is needed.</p>
</div>
      <div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
    </div>
  </div>

  <div id="right">
    <div id="rightContent">
      <div id="leftClose">
        <a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
      </div>
      <h1>Search:</h1>
      <p>
        <form action="search.html" name="perldoc_search">
	  <input type="text" name="q" size="10" class="grey"><br>
	  <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
	</form>
      </p>
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
      <h2>Labels:</h2>
      <p>
        <a href="#" onClick="addLabel('overload','overload.html')">Add this page</a>
      </p>
      <div class="labels" id="labels">
      </div>
    </div>
  </div>

</div>

</body>
</html>