source: trunk/third/perl/lib/overload.pm @ 14545

Revision 14545, 42.7 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r14544, which included commits to RCS files with non-trunk default branches.
Line 
1package overload;
2
3$overload::hint_bits = 0x20000;
4
5sub nil {}
6
7sub OVERLOAD {
8  $package = shift;
9  my %arg = @_;
10  my ($sub, $fb);
11  $ {$package . "::OVERLOAD"}{dummy}++; # Register with magic by touching.
12  *{$package . "::()"} = \&nil; # Make it findable via fetchmethod.
13  for (keys %arg) {
14    if ($_ eq 'fallback') {
15      $fb = $arg{$_};
16    } else {
17      $sub = $arg{$_};
18      if (not ref $sub and $sub !~ /::/) {
19        $ {$package . "::(" . $_} = $sub;
20        $sub = \&nil;
21      }
22      #print STDERR "Setting `$ {'package'}::\cO$_' to \\&`$sub'.\n";
23      *{$package . "::(" . $_} = \&{ $sub };
24    }
25  }
26  ${$package . "::()"} = $fb; # Make it findable too (fallback only).
27}
28
29sub import {
30  $package = (caller())[0];
31  # *{$package . "::OVERLOAD"} = \&OVERLOAD;
32  shift;
33  $package->overload::OVERLOAD(@_);
34}
35
36sub unimport {
37  $package = (caller())[0];
38  ${$package . "::OVERLOAD"}{dummy}++; # Upgrade the table
39  shift;
40  for (@_) {
41    if ($_ eq 'fallback') {
42      undef $ {$package . "::()"};
43    } else {
44      delete $ {$package . "::"}{"(" . $_};
45    }
46  }
47}
48
49sub Overloaded {
50  my $package = shift;
51  $package = ref $package if ref $package;
52  $package->can('()');
53}
54
55sub ov_method {
56  my $globref = shift;
57  return undef unless $globref;
58  my $sub = \&{*$globref};
59  return $sub if $sub ne \&nil;
60  return shift->can($ {*$globref});
61}
62
63sub OverloadedStringify {
64  my $package = shift;
65  $package = ref $package if ref $package;
66  #$package->can('(""')
67  ov_method mycan($package, '(""'), $package
68    or ov_method mycan($package, '(0+'), $package
69    or ov_method mycan($package, '(bool'), $package
70    or ov_method mycan($package, '(nomethod'), $package;
71}
72
73sub Method {
74  my $package = shift;
75  $package = ref $package if ref $package;
76  #my $meth = $package->can('(' . shift);
77  ov_method mycan($package, '(' . shift), $package;
78  #return $meth if $meth ne \&nil;
79  #return $ {*{$meth}};
80}
81
82sub AddrRef {
83  my $package = ref $_[0];
84  return "$_[0]" unless $package;
85  bless $_[0], overload::Fake;  # Non-overloaded package
86  my $str = "$_[0]";
87  bless $_[0], $package;        # Back
88  $package . substr $str, index $str, '=';
89}
90
91sub StrVal {
92  (OverloadedStringify($_[0]) or ref($_[0]) eq 'Regexp') ?
93    (AddrRef(shift)) :
94    "$_[0]";
95}
96
97sub mycan {                             # Real can would leave stubs.
98  my ($package, $meth) = @_;
99  return \*{$package . "::$meth"} if defined &{$package . "::$meth"};
100  my $p;
101  foreach $p (@{$package . "::ISA"}) {
102    my $out = mycan($p, $meth);
103    return $out if $out;
104  }
105  return undef;
106}
107
108%constants = (
109              'integer'   =>  0x1000,
110              'float'     =>  0x2000,
111              'binary'    =>  0x4000,
112              'q'         =>  0x8000,
113              'qr'        => 0x10000,
114             );
115
116%ops = ( with_assign      => "+ - * / % ** << >> x .",
117         assign           => "+= -= *= /= %= **= <<= >>= x= .=",
118         num_comparison   => "< <= >  >= == !=",
119         '3way_comparison'=> "<=> cmp",
120         str_comparison   => "lt le gt ge eq ne",
121         binary           => "& | ^",
122         unary            => "neg ! ~",
123         mutators         => '++ --',
124         func             => "atan2 cos sin exp abs log sqrt",
125         conversion       => 'bool "" 0+',
126         iterators        => '<>',
127         dereferencing    => '${} @{} %{} &{} *{}',
128         special          => 'nomethod fallback =');
129
130sub constant {
131  # Arguments: what, sub
132  while (@_) {
133    $^H{$_[0]} = $_[1];
134    $^H |= $constants{$_[0]} | $overload::hint_bits;
135    shift, shift;
136  }
137}
138
139sub remove_constant {
140  # Arguments: what, sub
141  while (@_) {
142    delete $^H{$_[0]};
143    $^H &= ~ $constants{$_[0]};
144    shift, shift;
145  }
146}
147
1481;
149
150__END__
151
152=head1 NAME
153
154overload - Package for overloading perl operations
155
156=head1 SYNOPSIS
157
158    package SomeThing;
159
160    use overload
161        '+' => \&myadd,
162        '-' => \&mysub;
163        # etc
164    ...
165
166    package main;
167    $a = new SomeThing 57;
168    $b=5+$a;
169    ...
170    if (overload::Overloaded $b) {...}
171    ...
172    $strval = overload::StrVal $b;
173
174=head1 DESCRIPTION
175
176=head2 Declaration of overloaded functions
177
178The compilation directive
179
180    package Number;
181    use overload
182        "+" => \&add,
183        "*=" => "muas";
184
185declares function Number::add() for addition, and method muas() in
186the "class" C<Number> (or one of its base classes)
187for the assignment form C<*=> of multiplication. 
188
189Arguments of this directive come in (key, value) pairs.  Legal values
190are values legal inside a C<&{ ... }> call, so the name of a
191subroutine, a reference to a subroutine, or an anonymous subroutine
192will all work.  Note that values specified as strings are
193interpreted as methods, not subroutines.  Legal keys are listed below.
194
195The subroutine C<add> will be called to execute C<$a+$b> if $a
196is a reference to an object blessed into the package C<Number>, or if $a is
197not an object from a package with defined mathemagic addition, but $b is a
198reference to a C<Number>.  It can also be called in other situations, like
199C<$a+=7>, or C<$a++>.  See L<MAGIC AUTOGENERATION>.  (Mathemagical
200methods refer to methods triggered by an overloaded mathematical
201operator.)
202
203Since overloading respects inheritance via the @ISA hierarchy, the
204above declaration would also trigger overloading of C<+> and C<*=> in
205all the packages which inherit from C<Number>.
206
207=head2 Calling Conventions for Binary Operations
208
209The functions specified in the C<use overload ...> directive are called
210with three (in one particular case with four, see L<Last Resort>)
211arguments.  If the corresponding operation is binary, then the first
212two arguments are the two arguments of the operation.  However, due to
213general object calling conventions, the first argument should always be
214an object in the package, so in the situation of C<7+$a>, the
215order of the arguments is interchanged.  It probably does not matter
216when implementing the addition method, but whether the arguments
217are reversed is vital to the subtraction method.  The method can
218query this information by examining the third argument, which can take
219three different values:
220
221=over 7
222
223=item FALSE
224
225the order of arguments is as in the current operation.
226
227=item TRUE
228
229the arguments are reversed.
230
231=item C<undef>
232
233the current operation is an assignment variant (as in
234C<$a+=7>), but the usual function is called instead.  This additional
235information can be used to generate some optimizations.  Compare
236L<Calling Conventions for Mutators>.
237
238=back
239
240=head2 Calling Conventions for Unary Operations
241
242Unary operation are considered binary operations with the second
243argument being C<undef>.  Thus the functions that overloads C<{"++"}>
244is called with arguments C<($a,undef,'')> when $a++ is executed.
245
246=head2 Calling Conventions for Mutators
247
248Two types of mutators have different calling conventions:
249
250=over
251
252=item C<++> and C<-->
253
254The routines which implement these operators are expected to actually
255I<mutate> their arguments.  So, assuming that $obj is a reference to a
256number,
257
258  sub incr { my $n = $ {$_[0]}; ++$n; $_[0] = bless \$n}
259
260is an appropriate implementation of overloaded C<++>.  Note that
261
262  sub incr { ++$ {$_[0]} ; shift }
263
264is OK if used with preincrement and with postincrement. (In the case
265of postincrement a copying will be performed, see L<Copy Constructor>.)
266
267=item C<x=> and other assignment versions
268
269There is nothing special about these methods.  They may change the
270value of their arguments, and may leave it as is.  The result is going
271to be assigned to the value in the left-hand-side if different from
272this value.
273
274This allows for the same method to be used as overloaded C<+=> and
275C<+>.  Note that this is I<allowed>, but not recommended, since by the
276semantic of L<"Fallback"> Perl will call the method for C<+> anyway,
277if C<+=> is not overloaded.
278
279=back
280
281B<Warning.>  Due to the presense of assignment versions of operations,
282routines which may be called in assignment context may create
283self-referential structures.  Currently Perl will not free self-referential
284structures until cycles are C<explicitly> broken.  You may get problems
285when traversing your structures too.
286
287Say,
288
289  use overload '+' => sub { bless [ \$_[0], \$_[1] ] };
290
291is asking for trouble, since for code C<$obj += $foo> the subroutine
292is called as C<$obj = add($obj, $foo, undef)>, or C<$obj = [\$obj,
293\$foo]>.  If using such a subroutine is an important optimization, one
294can overload C<+=> explicitly by a non-"optimized" version, or switch
295to non-optimized version if C<not defined $_[2]> (see
296L<Calling Conventions for Binary Operations>).
297
298Even if no I<explicit> assignment-variants of operators are present in
299the script, they may be generated by the optimizer.  Say, C<",$obj,"> or
300C<',' . $obj . ','> may be both optimized to
301
302  my $tmp = ',' . $obj;    $tmp .= ',';
303
304=head2 Overloadable Operations
305
306The following symbols can be specified in C<use overload> directive:
307
308=over 5
309
310=item * I<Arithmetic operations>
311
312    "+", "+=", "-", "-=", "*", "*=", "/", "/=", "%", "%=",
313    "**", "**=", "<<", "<<=", ">>", ">>=", "x", "x=", ".", ".=",
314
315For these operations a substituted non-assignment variant can be called if
316the assignment variant is not available.  Methods for operations "C<+>",
317"C<->", "C<+=>", and "C<-=>" can be called to automatically generate
318increment and decrement methods.  The operation "C<->" can be used to
319autogenerate missing methods for unary minus or C<abs>.
320
321See L<"MAGIC AUTOGENERATION">, L<"Calling Conventions for Mutators"> and
322L<"Calling Conventions for Binary Operations">) for details of these
323substitutions.
324
325=item * I<Comparison operations>
326
327    "<",  "<=", ">",  ">=", "==", "!=", "<=>",
328    "lt", "le", "gt", "ge", "eq", "ne", "cmp",
329
330If the corresponding "spaceship" variant is available, it can be
331used to substitute for the missing operation.  During C<sort>ing
332arrays, C<cmp> is used to compare values subject to C<use overload>.
333
334=item * I<Bit operations>
335
336    "&", "^", "|", "neg", "!", "~",
337
338"C<neg>" stands for unary minus.  If the method for C<neg> is not
339specified, it can be autogenerated using the method for
340subtraction. If the method for "C<!>" is not specified, it can be
341autogenerated using the methods for "C<bool>", or "C<\"\">", or "C<0+>".
342
343=item * I<Increment and decrement>
344
345    "++", "--",
346
347If undefined, addition and subtraction methods can be
348used instead.  These operations are called both in prefix and
349postfix form.
350
351=item * I<Transcendental functions>
352
353    "atan2", "cos", "sin", "exp", "abs", "log", "sqrt",
354
355If C<abs> is unavailable, it can be autogenerated using methods
356for "E<lt>" or "E<lt>=E<gt>" combined with either unary minus or subtraction.
357
358=item * I<Boolean, string and numeric conversion>
359
360    "bool", "\"\"", "0+",
361
362If one or two of these operations are not overloaded, the remaining ones can
363be used instead.  C<bool> is used in the flow control operators
364(like C<while>) and for the ternary "C<?:>" operation.  These functions can
365return any arbitrary Perl value.  If the corresponding operation for this value
366is overloaded too, that operation will be called again with this value.
367
368=item * I<Iteration>
369
370    "<>"
371
372If not overloaded, the argument will be converted to a filehandle or
373glob (which may require a stringification).  The same overloading
374happens both for the I<read-filehandle> syntax C<E<lt>$varE<gt>> and
375I<globbing> syntax C<E<lt>${var}E<gt>>.
376
377=item * I<Dereferencing>
378
379    '${}', '@{}', '%{}', '&{}', '*{}'.
380
381If not overloaded, the argument will be dereferenced I<as is>, thus
382should be of correct type.  These functions should return a reference
383of correct type, or another object with overloaded dereferencing.
384
385=item * I<Special>
386
387    "nomethod", "fallback", "=",
388
389see L<SPECIAL SYMBOLS FOR C<use overload>>.
390
391=back
392
393See L<"Fallback"> for an explanation of when a missing method can be
394autogenerated.
395
396A computer-readable form of the above table is available in the hash
397%overload::ops, with values being space-separated lists of names:
398
399 with_assign      => '+ - * / % ** << >> x .',
400 assign           => '+= -= *= /= %= **= <<= >>= x= .=',
401 num_comparison   => '< <= > >= == !=',
402 '3way_comparison'=> '<=> cmp',
403 str_comparison   => 'lt le gt ge eq ne',
404 binary           => '& | ^',
405 unary            => 'neg ! ~',
406 mutators         => '++ --',
407 func             => 'atan2 cos sin exp abs log sqrt',
408 conversion       => 'bool "" 0+',
409 iterators        => '<>',
410 dereferencing    => '${} @{} %{} &{} *{}',
411 special          => 'nomethod fallback ='
412
413=head2 Inheritance and overloading
414
415Inheritance interacts with overloading in two ways.
416
417=over
418
419=item Strings as values of C<use overload> directive
420
421If C<value> in
422
423  use overload key => value;
424
425is a string, it is interpreted as a method name.
426
427=item Overloading of an operation is inherited by derived classes
428
429Any class derived from an overloaded class is also overloaded.  The
430set of overloaded methods is the union of overloaded methods of all
431the ancestors. If some method is overloaded in several ancestor, then
432which description will be used is decided by the usual inheritance
433rules:
434
435If C<A> inherits from C<B> and C<C> (in this order), C<B> overloads
436C<+> with C<\&D::plus_sub>, and C<C> overloads C<+> by C<"plus_meth">,
437then the subroutine C<D::plus_sub> will be called to implement
438operation C<+> for an object in package C<A>.
439
440=back
441
442Note that since the value of the C<fallback> key is not a subroutine,
443its inheritance is not governed by the above rules.  In the current
444implementation, the value of C<fallback> in the first overloaded
445ancestor is used, but this is accidental and subject to change.
446
447=head1 SPECIAL SYMBOLS FOR C<use overload>
448
449Three keys are recognized by Perl that are not covered by the above
450description.
451
452=head2 Last Resort
453
454C<"nomethod"> should be followed by a reference to a function of four
455parameters.  If defined, it is called when the overloading mechanism
456cannot find a method for some operation.  The first three arguments of
457this function coincide with the arguments for the corresponding method if
458it were found, the fourth argument is the symbol
459corresponding to the missing method.  If several methods are tried,
460the last one is used.  Say, C<1-$a> can be equivalent to
461
462        &nomethodMethod($a,1,1,"-")
463
464if the pair C<"nomethod" =E<gt> "nomethodMethod"> was specified in the
465C<use overload> directive.
466
467If some operation cannot be resolved, and there is no function
468assigned to C<"nomethod">, then an exception will be raised via die()--
469unless C<"fallback"> was specified as a key in C<use overload> directive.
470
471=head2 Fallback
472
473The key C<"fallback"> governs what to do if a method for a particular
474operation is not found.  Three different cases are possible depending on
475the value of C<"fallback">:
476
477=over 16
478
479=item * C<undef>
480
481Perl tries to use a
482substituted method (see L<MAGIC AUTOGENERATION>).  If this fails, it
483then tries to calls C<"nomethod"> value; if missing, an exception
484will be raised.
485
486=item * TRUE
487
488The same as for the C<undef> value, but no exception is raised.  Instead,
489it silently reverts to what it would have done were there no C<use overload>
490present.
491
492=item * defined, but FALSE
493
494No autogeneration is tried.  Perl tries to call
495C<"nomethod"> value, and if this is missing, raises an exception.
496
497=back
498
499B<Note.> C<"fallback"> inheritance via @ISA is not carved in stone
500yet, see L<"Inheritance and overloading">.
501
502=head2 Copy Constructor
503
504The value for C<"="> is a reference to a function with three
505arguments, i.e., it looks like the other values in C<use
506overload>. However, it does not overload the Perl assignment
507operator. This would go against Camel hair.
508
509This operation is called in the situations when a mutator is applied
510to a reference that shares its object with some other reference, such
511as
512
513        $a=$b;
514        ++$a;
515
516To make this change $a and not change $b, a copy of C<$$a> is made,
517and $a is assigned a reference to this new object.  This operation is
518done during execution of the C<++$a>, and not during the assignment,
519(so before the increment C<$$a> coincides with C<$$b>).  This is only
520done if C<++> is expressed via a method for C<'++'> or C<'+='> (or
521C<nomethod>).  Note that if this operation is expressed via C<'+'>
522a nonmutator, i.e., as in
523
524        $a=$b;
525        $a=$a+1;
526
527then C<$a> does not reference a new copy of C<$$a>, since $$a does not
528appear as lvalue when the above code is executed.
529
530If the copy constructor is required during the execution of some mutator,
531but a method for C<'='> was not specified, it can be autogenerated as a
532string copy if the object is a plain scalar.
533
534=over 5
535
536=item B<Example>
537
538The actually executed code for
539
540        $a=$b;
541        Something else which does not modify $a or $b....
542        ++$a;
543
544may be
545
546        $a=$b;
547        Something else which does not modify $a or $b....
548        $a = $a->clone(undef,"");
549        $a->incr(undef,"");
550
551if $b was mathemagical, and C<'++'> was overloaded with C<\&incr>,
552C<'='> was overloaded with C<\&clone>.
553
554=back
555
556Same behaviour is triggered by C<$b = $a++>, which is consider a synonym for
557C<$b = $a; ++$a>.
558
559=head1 MAGIC AUTOGENERATION
560
561If a method for an operation is not found, and the value for  C<"fallback"> is
562TRUE or undefined, Perl tries to autogenerate a substitute method for
563the missing operation based on the defined operations.  Autogenerated method
564substitutions are possible for the following operations:
565
566=over 16
567
568=item I<Assignment forms of arithmetic operations>
569
570C<$a+=$b> can use the method for C<"+"> if the method for C<"+=">
571is not defined.
572
573=item I<Conversion operations>
574
575String, numeric, and boolean conversion are calculated in terms of one
576another if not all of them are defined.
577
578=item I<Increment and decrement>
579
580The C<++$a> operation can be expressed in terms of C<$a+=1> or C<$a+1>,
581and C<$a--> in terms of C<$a-=1> and C<$a-1>.
582
583=item C<abs($a)>
584
585can be expressed in terms of C<$aE<lt>0> and C<-$a> (or C<0-$a>).
586
587=item I<Unary minus>
588
589can be expressed in terms of subtraction.
590
591=item I<Negation>
592
593C<!> and C<not> can be expressed in terms of boolean conversion, or
594string or numerical conversion.
595
596=item I<Concatenation>
597
598can be expressed in terms of string conversion.
599
600=item I<Comparison operations>
601
602can be expressed in terms of its "spaceship" counterpart: either
603C<E<lt>=E<gt>> or C<cmp>:
604
605    <, >, <=, >=, ==, !=        in terms of <=>
606    lt, gt, le, ge, eq, ne      in terms of cmp
607
608=item I<Iterator>
609
610    <>                          in terms of builtin operations
611
612=item I<Dereferencing>
613
614    ${} @{} %{} &{} *{}         in terms of builtin operations
615
616=item I<Copy operator>
617
618can be expressed in terms of an assignment to the dereferenced value, if this
619value is a scalar and not a reference.
620
621=back
622
623=head1 Losing overloading
624
625The restriction for the comparison operation is that even if, for example,
626`C<cmp>' should return a blessed reference, the autogenerated `C<lt>'
627function will produce only a standard logical value based on the
628numerical value of the result of `C<cmp>'.  In particular, a working
629numeric conversion is needed in this case (possibly expressed in terms of
630other conversions).
631
632Similarly, C<.=>  and C<x=> operators lose their mathemagical properties
633if the string conversion substitution is applied.
634
635When you chop() a mathemagical object it is promoted to a string and its
636mathemagical properties are lost.  The same can happen with other
637operations as well.
638
639=head1 Run-time Overloading
640
641Since all C<use> directives are executed at compile-time, the only way to
642change overloading during run-time is to
643
644    eval 'use overload "+" => \&addmethod';
645
646You can also use
647
648    eval 'no overload "+", "--", "<="';
649
650though the use of these constructs during run-time is questionable.
651
652=head1 Public functions
653
654Package C<overload.pm> provides the following public functions:
655
656=over 5
657
658=item overload::StrVal(arg)
659
660Gives string value of C<arg> as in absence of stringify overloading.
661
662=item overload::Overloaded(arg)
663
664Returns true if C<arg> is subject to overloading of some operations.
665
666=item overload::Method(obj,op)
667
668Returns C<undef> or a reference to the method that implements C<op>.
669
670=back
671
672=head1 Overloading constants
673
674For some application Perl parser mangles constants too much.  It is possible
675to hook into this process via overload::constant() and overload::remove_constant()
676functions.
677
678These functions take a hash as an argument.  The recognized keys of this hash
679are
680
681=over 8
682
683=item integer
684
685to overload integer constants,
686
687=item float
688
689to overload floating point constants,
690
691=item binary
692
693to overload octal and hexadecimal constants,
694
695=item q
696
697to overload C<q>-quoted strings, constant pieces of C<qq>- and C<qx>-quoted
698strings and here-documents,
699
700=item qr
701
702to overload constant pieces of regular expressions.
703
704=back
705
706The corresponding values are references to functions which take three arguments:
707the first one is the I<initial> string form of the constant, the second one
708is how Perl interprets this constant, the third one is how the constant is used. 
709Note that the initial string form does not
710contain string delimiters, and has backslashes in backslash-delimiter
711combinations stripped (thus the value of delimiter is not relevant for
712processing of this string).  The return value of this function is how this
713constant is going to be interpreted by Perl.  The third argument is undefined
714unless for overloaded C<q>- and C<qr>- constants, it is C<q> in single-quote
715context (comes from strings, regular expressions, and single-quote HERE
716documents), it is C<tr> for arguments of C<tr>/C<y> operators,
717it is C<s> for right-hand side of C<s>-operator, and it is C<qq> otherwise.
718
719Since an expression C<"ab$cd,,"> is just a shortcut for C<'ab' . $cd . ',,'>,
720it is expected that overloaded constant strings are equipped with reasonable
721overloaded catenation operator, otherwise absurd results will result. 
722Similarly, negative numbers are considered as negations of positive constants.
723
724Note that it is probably meaningless to call the functions overload::constant()
725and overload::remove_constant() from anywhere but import() and unimport() methods.
726From these methods they may be called as
727
728        sub import {
729          shift;
730          return unless @_;
731          die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant';
732          overload::constant integer => sub {Math::BigInt->new(shift)};
733        }
734
735B<BUGS> Currently overloaded-ness of constants does not propagate
736into C<eval '...'>.
737
738=head1 IMPLEMENTATION
739
740What follows is subject to change RSN.
741
742The table of methods for all operations is cached in magic for the
743symbol table hash for the package.  The cache is invalidated during
744processing of C<use overload>, C<no overload>, new function
745definitions, and changes in @ISA. However, this invalidation remains
746unprocessed until the next C<bless>ing into the package. Hence if you
747want to change overloading structure dynamically, you'll need an
748additional (fake) C<bless>ing to update the table.
749
750(Every SVish thing has a magic queue, and magic is an entry in that
751queue.  This is how a single variable may participate in multiple
752forms of magic simultaneously.  For instance, environment variables
753regularly have two forms at once: their %ENV magic and their taint
754magic. However, the magic which implements overloading is applied to
755the stashes, which are rarely used directly, thus should not slow down
756Perl.)
757
758If an object belongs to a package using overload, it carries a special
759flag.  Thus the only speed penalty during arithmetic operations without
760overloading is the checking of this flag.
761
762In fact, if C<use overload> is not present, there is almost no overhead
763for overloadable operations, so most programs should not suffer
764measurable performance penalties.  A considerable effort was made to
765minimize the overhead when overload is used in some package, but the
766arguments in question do not belong to packages using overload.  When
767in doubt, test your speed with C<use overload> and without it.  So far
768there have been no reports of substantial speed degradation if Perl is
769compiled with optimization turned on.
770
771There is no size penalty for data if overload is not used. The only
772size penalty if overload is used in some package is that I<all> the
773packages acquire a magic during the next C<bless>ing into the
774package. This magic is three-words-long for packages without
775overloading, and carries the cache table if the package is overloaded.
776
777Copying (C<$a=$b>) is shallow; however, a one-level-deep copying is
778carried out before any operation that can imply an assignment to the
779object $a (or $b) refers to, like C<$a++>.  You can override this
780behavior by defining your own copy constructor (see L<"Copy Constructor">).
781
782It is expected that arguments to methods that are not explicitly supposed
783to be changed are constant (but this is not enforced).
784
785=head1 Metaphor clash
786
787One may wonder why the semantic of overloaded C<=> is so counter intuitive.
788If it I<looks> counter intuitive to you, you are subject to a metaphor
789clash. 
790
791Here is a Perl object metaphor:
792
793I<  object is a reference to blessed data>
794
795and an arithmetic metaphor:
796
797I<  object is a thing by itself>.
798
799The I<main> problem of overloading C<=> is the fact that these metaphors
800imply different actions on the assignment C<$a = $b> if $a and $b are
801objects.  Perl-think implies that $a becomes a reference to whatever
802$b was referencing.  Arithmetic-think implies that the value of "object"
803$a is changed to become the value of the object $b, preserving the fact
804that $a and $b are separate entities.
805
806The difference is not relevant in the absence of mutators.  After
807a Perl-way assignment an operation which mutates the data referenced by $a
808would change the data referenced by $b too.  Effectively, after
809C<$a = $b> values of $a and $b become I<indistinguishable>.
810
811On the other hand, anyone who has used algebraic notation knows the
812expressive power of the arithmetic metaphor.  Overloading works hard
813to enable this metaphor while preserving the Perlian way as far as
814possible.  Since it is not not possible to freely mix two contradicting
815metaphors, overloading allows the arithmetic way to write things I<as
816far as all the mutators are called via overloaded access only>.  The
817way it is done is described in L<Copy Constructor>.
818
819If some mutator methods are directly applied to the overloaded values,
820one may need to I<explicitly unlink> other values which references the
821same value:
822
823    $a = new Data 23;
824    ...
825    $b = $a;            # $b is "linked" to $a
826    ...
827    $a = $a->clone;     # Unlink $b from $a
828    $a->increment_by(4);
829
830Note that overloaded access makes this transparent:
831
832    $a = new Data 23;
833    $b = $a;            # $b is "linked" to $a
834    $a += 4;            # would unlink $b automagically
835
836However, it would not make
837
838    $a = new Data 23;
839    $a = 4;             # Now $a is a plain 4, not 'Data'
840
841preserve "objectness" of $a.  But Perl I<has> a way to make assignments
842to an object do whatever you want.  It is just not the overload, but
843tie()ing interface (see L<perlfunc/tie>).  Adding a FETCH() method
844which returns the object itself, and STORE() method which changes the
845value of the object, one can reproduce the arithmetic metaphor in its
846completeness, at least for variables which were tie()d from the start.
847
848(Note that a workaround for a bug may be needed, see L<"BUGS">.)
849
850=head1 Cookbook
851
852Please add examples to what follows!
853
854=head2 Two-face scalars
855
856Put this in F<two_face.pm> in your Perl library directory:
857
858  package two_face;             # Scalars with separate string and
859                                # numeric values.
860  sub new { my $p = shift; bless [@_], $p }
861  use overload '""' => \&str, '0+' => \&num, fallback => 1;
862  sub num {shift->[1]}
863  sub str {shift->[0]}
864
865Use it as follows:
866
867  require two_face;
868  my $seven = new two_face ("vii", 7);
869  printf "seven=$seven, seven=%d, eight=%d\n", $seven, $seven+1;
870  print "seven contains `i'\n" if $seven =~ /i/;
871
872(The second line creates a scalar which has both a string value, and a
873numeric value.)  This prints:
874
875  seven=vii, seven=7, eight=8
876  seven contains `i'
877
878=head2 Two-face references
879
880Suppose you want to create an object which is accessible as both an
881array reference, and a hash reference, similar to the builtin
882L<array-accessible-as-a-hash|perlref/"Pseudo-hashes: Using an array as
883a hash"> builtin Perl type.  Let us make it better than the builtin
884type, there will be no restriction that you cannot use the index 0 of
885your array.
886
887  package two_refs;
888  use overload '%{}' => \&gethash, '@{}' => sub { $ {shift()} };
889  sub new {
890    my $p = shift;
891    bless \ [@_], $p;
892  }
893  sub gethash {
894    my %h;
895    my $self = shift;
896    tie %h, ref $self, $self;
897    \%h;
898  }
899
900  sub TIEHASH { my $p = shift; bless \ shift, $p }
901  my %fields;
902  my $i = 0;
903  $fields{$_} = $i++ foreach qw{zero one two three};
904  sub STORE {
905    my $self = ${shift()};
906    my $key = $fields{shift()};
907    defined $key or die "Out of band access";
908    $$self->[$key] = shift;
909  }
910  sub FETCH {
911    my $self = ${shift()};
912    my $key = $fields{shift()};
913    defined $key or die "Out of band access";
914    $$self->[$key];
915  }
916
917Now one can access an object using both the array and hash syntax:
918
919  my $bar = new two_refs 3,4,5,6;
920  $bar->[2] = 11;
921  $bar->{two} == 11 or die 'bad hash fetch';
922
923Note several important features of this example.  First of all, the
924I<actual> type of $bar is a scalar reference, and we do not overload
925the scalar dereference.  Thus we can get the I<actual> non-overloaded
926contents of $bar by just using C<$$bar> (what we do in functions which
927overload dereference).  Similarly, the object returned by the
928TIEHASH() method is a scalar reference.
929
930Second, we create a new tied hash each time the hash syntax is used.
931This allows us not to worry about a possibility of a reference loop,
932would would lead to a memory leak.
933
934Both these problems can be cured.  Say, if we want to overload hash
935dereference on a reference to an object which is I<implemented> as a
936hash itself, the only problem one has to circumvent is how to access
937this I<actual> hash (as opposed to the I<virtual> exhibited by
938overloaded dereference operator).  Here is one possible fetching routine:
939
940  sub access_hash {
941    my ($self, $key) = (shift, shift);
942    my $class = ref $self;
943    bless $self, 'overload::dummy'; # Disable overloading of %{}
944    my $out = $self->{$key};
945    bless $self, $class;        # Restore overloading
946    $out;
947  }
948
949To move creation of the tied hash on each access, one may an extra
950level of indirection which allows a non-circular structure of references:
951
952  package two_refs1;
953  use overload '%{}' => sub { ${shift()}->[1] },
954               '@{}' => sub { ${shift()}->[0] };
955  sub new {
956    my $p = shift;
957    my $a = [@_];
958    my %h;
959    tie %h, $p, $a;
960    bless \ [$a, \%h], $p;
961  }
962  sub gethash {
963    my %h;
964    my $self = shift;
965    tie %h, ref $self, $self;
966    \%h;
967  }
968
969  sub TIEHASH { my $p = shift; bless \ shift, $p }
970  my %fields;
971  my $i = 0;
972  $fields{$_} = $i++ foreach qw{zero one two three};
973  sub STORE {
974    my $a = ${shift()};
975    my $key = $fields{shift()};
976    defined $key or die "Out of band access";
977    $a->[$key] = shift;
978  }
979  sub FETCH {
980    my $a = ${shift()};
981    my $key = $fields{shift()};
982    defined $key or die "Out of band access";
983    $a->[$key];
984  }
985
986Now if $baz is overloaded like this, then C<$bar> is a reference to a
987reference to the intermediate array, which keeps a reference to an
988actual array, and the access hash.  The tie()ing object for the access
989hash is also a reference to a reference to the actual array, so
990
991=over
992
993=item *
994
995There are no loops of references.
996
997=item *
998
999Both "objects" which are blessed into the class C<two_refs1> are
1000references to a reference to an array, thus references to a I<scalar>.
1001Thus the accessor expression C<$$foo-E<gt>[$ind]> involves no
1002overloaded operations.
1003
1004=back
1005
1006=head2 Symbolic calculator
1007
1008Put this in F<symbolic.pm> in your Perl library directory:
1009
1010  package symbolic;             # Primitive symbolic calculator
1011  use overload nomethod => \&wrap;
1012
1013  sub new { shift; bless ['n', @_] }
1014  sub wrap {
1015    my ($obj, $other, $inv, $meth) = @_;
1016    ($obj, $other) = ($other, $obj) if $inv;
1017    bless [$meth, $obj, $other];
1018  }
1019
1020This module is very unusual as overloaded modules go: it does not
1021provide any usual overloaded operators, instead it provides the L<Last
1022Resort> operator C<nomethod>.  In this example the corresponding
1023subroutine returns an object which encapsulates operations done over
1024the objects: C<new symbolic 3> contains C<['n', 3]>, C<2 + new
1025symbolic 3> contains C<['+', 2, ['n', 3]]>.
1026
1027Here is an example of the script which "calculates" the side of
1028circumscribed octagon using the above package:
1029
1030  require symbolic;
1031  my $iter = 1;                 # 2**($iter+2) = 8
1032  my $side = new symbolic 1;
1033  my $cnt = $iter;
1034
1035  while ($cnt--) {
1036    $side = (sqrt(1 + $side**2) - 1)/$side;
1037  }
1038  print "OK\n";
1039
1040The value of $side is
1041
1042  ['/', ['-', ['sqrt', ['+', 1, ['**', ['n', 1], 2]],
1043                       undef], 1], ['n', 1]]
1044
1045Note that while we obtained this value using a nice little script,
1046there is no simple way to I<use> this value.  In fact this value may
1047be inspected in debugger (see L<perldebug>), but ony if
1048C<bareStringify> B<O>ption is set, and not via C<p> command.
1049
1050If one attempts to print this value, then the overloaded operator
1051C<""> will be called, which will call C<nomethod> operator.  The
1052result of this operator will be stringified again, but this result is
1053again of type C<symbolic>, which will lead to an infinite loop.
1054
1055Add a pretty-printer method to the module F<symbolic.pm>:
1056
1057  sub pretty {
1058    my ($meth, $a, $b) = @{+shift};
1059    $a = 'u' unless defined $a;
1060    $b = 'u' unless defined $b;
1061    $a = $a->pretty if ref $a;
1062    $b = $b->pretty if ref $b;
1063    "[$meth $a $b]";
1064  }
1065
1066Now one can finish the script by
1067
1068  print "side = ", $side->pretty, "\n";
1069
1070The method C<pretty> is doing object-to-string conversion, so it
1071is natural to overload the operator C<""> using this method.  However,
1072inside such a method it is not necessary to pretty-print the
1073I<components> $a and $b of an object.  In the above subroutine
1074C<"[$meth $a $b]"> is a catenation of some strings and components $a
1075and $b.  If these components use overloading, the catenation operator
1076will look for an overloaded operator C<.>, if not present, it will
1077look for an overloaded operator C<"">.  Thus it is enough to use
1078
1079  use overload nomethod => \&wrap, '""' => \&str;
1080  sub str {
1081    my ($meth, $a, $b) = @{+shift};
1082    $a = 'u' unless defined $a;
1083    $b = 'u' unless defined $b;
1084    "[$meth $a $b]";
1085  }
1086
1087Now one can change the last line of the script to
1088
1089  print "side = $side\n";
1090
1091which outputs
1092
1093  side = [/ [- [sqrt [+ 1 [** [n 1 u] 2]] u] 1] [n 1 u]]
1094
1095and one can inspect the value in debugger using all the possible
1096methods. 
1097
1098Something is is still amiss: consider the loop variable $cnt of the
1099script.  It was a number, not an object.  We cannot make this value of
1100type C<symbolic>, since then the loop will not terminate.
1101
1102Indeed, to terminate the cycle, the $cnt should become false.
1103However, the operator C<bool> for checking falsity is overloaded (this
1104time via overloaded C<"">), and returns a long string, thus any object
1105of type C<symbolic> is true.  To overcome this, we need a way to
1106compare an object to 0.  In fact, it is easier to write a numeric
1107conversion routine.
1108
1109Here is the text of F<symbolic.pm> with such a routine added (and
1110slightly modified str()):
1111
1112  package symbolic;             # Primitive symbolic calculator
1113  use overload
1114    nomethod => \&wrap, '""' => \&str, '0+' => \&num;
1115
1116  sub new { shift; bless ['n', @_] }
1117  sub wrap {
1118    my ($obj, $other, $inv, $meth) = @_;
1119    ($obj, $other) = ($other, $obj) if $inv;
1120    bless [$meth, $obj, $other];
1121  }
1122  sub str {
1123    my ($meth, $a, $b) = @{+shift};
1124    $a = 'u' unless defined $a;
1125    if (defined $b) {
1126      "[$meth $a $b]";
1127    } else {
1128      "[$meth $a]";
1129    }
1130  }
1131  my %subr = ( n => sub {$_[0]},
1132               sqrt => sub {sqrt $_[0]},
1133               '-' => sub {shift() - shift()},
1134               '+' => sub {shift() + shift()},
1135               '/' => sub {shift() / shift()},
1136               '*' => sub {shift() * shift()},
1137               '**' => sub {shift() ** shift()},
1138             );
1139  sub num {
1140    my ($meth, $a, $b) = @{+shift};
1141    my $subr = $subr{$meth}
1142      or die "Do not know how to ($meth) in symbolic";
1143    $a = $a->num if ref $a eq __PACKAGE__;
1144    $b = $b->num if ref $b eq __PACKAGE__;
1145    $subr->($a,$b);
1146  }
1147
1148All the work of numeric conversion is done in %subr and num().  Of
1149course, %subr is not complete, it contains only operators used in the
1150example below.  Here is the extra-credit question: why do we need an
1151explicit recursion in num()?  (Answer is at the end of this section.)
1152
1153Use this module like this:
1154
1155  require symbolic;
1156  my $iter = new symbolic 2;    # 16-gon
1157  my $side = new symbolic 1;
1158  my $cnt = $iter;
1159
1160  while ($cnt) {
1161    $cnt = $cnt - 1;            # Mutator `--' not implemented
1162    $side = (sqrt(1 + $side**2) - 1)/$side;
1163  }
1164  printf "%s=%f\n", $side, $side;
1165  printf "pi=%f\n", $side*(2**($iter+2));
1166
1167It prints (without so many line breaks)
1168
1169  [/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1]
1170                          [n 1]] 2]]] 1]
1171     [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]=0.198912
1172  pi=3.182598
1173
1174The above module is very primitive.  It does not implement
1175mutator methods (C<++>, C<-=> and so on), does not do deep copying
1176(not required without mutators!), and implements only those arithmetic
1177operations which are used in the example.
1178
1179To implement most arithmetic operations is easy, one should just use
1180the tables of operations, and change the code which fills %subr to
1181
1182  my %subr = ( 'n' => sub {$_[0]} );
1183  foreach my $op (split " ", $overload::ops{with_assign}) {
1184    $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
1185  }
1186  my @bins = qw(binary 3way_comparison num_comparison str_comparison);
1187  foreach my $op (split " ", "@overload::ops{ @bins }") {
1188    $subr{$op} = eval "sub {shift() $op shift()}";
1189  }
1190  foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
1191    print "defining `$op'\n";
1192    $subr{$op} = eval "sub {$op shift()}";
1193  }
1194
1195Due to L<Calling Conventions for Mutators>, we do not need anything
1196special to make C<+=> and friends work, except filling C<+=> entry of
1197%subr, and defining a copy constructor (needed since Perl has no
1198way to know that the implementation of C<'+='> does not mutate
1199the argument, compare L<Copy Constructor>).
1200
1201To implement a copy constructor, add C<'=' => \&cpy> to C<use overload>
1202line, and code (this code assumes that mutators change things one level
1203deep only, so recursive copying is not needed):
1204
1205  sub cpy {
1206    my $self = shift;
1207    bless [@$self], ref $self;
1208  }
1209
1210To make C<++> and C<--> work, we need to implement actual mutators,
1211either directly, or in C<nomethod>.  We continue to do things inside
1212C<nomethod>, thus add
1213
1214    if ($meth eq '++' or $meth eq '--') {
1215      @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference
1216      return $obj;
1217    }
1218
1219after the first line of wrap().  This is not a most effective
1220implementation, one may consider
1221
1222  sub inc { $_[0] = bless ['++', shift, 1]; }
1223
1224instead.
1225
1226As a final remark, note that one can fill %subr by
1227
1228  my %subr = ( 'n' => sub {$_[0]} );
1229  foreach my $op (split " ", $overload::ops{with_assign}) {
1230    $subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
1231  }
1232  my @bins = qw(binary 3way_comparison num_comparison str_comparison);
1233  foreach my $op (split " ", "@overload::ops{ @bins }") {
1234    $subr{$op} = eval "sub {shift() $op shift()}";
1235  }
1236  foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
1237    $subr{$op} = eval "sub {$op shift()}";
1238  }
1239  $subr{'++'} = $subr{'+'};
1240  $subr{'--'} = $subr{'-'};
1241
1242This finishes implementation of a primitive symbolic calculator in
124350 lines of Perl code.  Since the numeric values of subexpressions
1244are not cached, the calculator is very slow.
1245
1246Here is the answer for the exercise: In the case of str(), we need no
1247explicit recursion since the overloaded C<.>-operator will fall back
1248to an existing overloaded operator C<"">.  Overloaded arithmetic
1249operators I<do not> fall back to numeric conversion if C<fallback> is
1250not explicitly requested.  Thus without an explicit recursion num()
1251would convert C<['+', $a, $b]> to C<$a + $b>, which would just rebuild
1252the argument of num().
1253
1254If you wonder why defaults for conversion are different for str() and
1255num(), note how easy it was to write the symbolic calculator.  This
1256simplicity is due to an appropriate choice of defaults.  One extra
1257note: due to the explicit recursion num() is more fragile than sym():
1258we need to explicitly check for the type of $a and $b.  If components
1259$a and $b happen to be of some related type, this may lead to problems.
1260
1261=head2 I<Really> symbolic calculator
1262
1263One may wonder why we call the above calculator symbolic.  The reason
1264is that the actual calculation of the value of expression is postponed
1265until the value is I<used>.
1266
1267To see it in action, add a method
1268
1269  sub STORE {
1270    my $obj = shift;
1271    $#$obj = 1;
1272    @$obj->[0,1] = ('=', shift);
1273  }
1274
1275to the package C<symbolic>.  After this change one can do
1276
1277  my $a = new symbolic 3;
1278  my $b = new symbolic 4;
1279  my $c = sqrt($a**2 + $b**2);
1280
1281and the numeric value of $c becomes 5.  However, after calling
1282
1283  $a->STORE(12);  $b->STORE(5);
1284
1285the numeric value of $c becomes 13.  There is no doubt now that the module
1286symbolic provides a I<symbolic> calculator indeed.
1287
1288To hide the rough edges under the hood, provide a tie()d interface to the
1289package C<symbolic> (compare with L<Metaphor clash>).  Add methods
1290
1291  sub TIESCALAR { my $pack = shift; $pack->new(@_) }
1292  sub FETCH { shift }
1293  sub nop {  }          # Around a bug
1294
1295(the bug is described in L<"BUGS">).  One can use this new interface as
1296
1297  tie $a, 'symbolic', 3;
1298  tie $b, 'symbolic', 4;
1299  $a->nop;  $b->nop;    # Around a bug
1300
1301  my $c = sqrt($a**2 + $b**2);
1302
1303Now numeric value of $c is 5.  After C<$a = 12; $b = 5> the numeric value
1304of $c becomes 13.  To insulate the user of the module add a method
1305
1306  sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
1307
1308Now
1309
1310  my ($a, $b);
1311  symbolic->vars($a, $b);
1312  my $c = sqrt($a**2 + $b**2);
1313
1314  $a = 3; $b = 4;
1315  printf "c5  %s=%f\n", $c, $c;
1316
1317  $a = 12; $b = 5;
1318  printf "c13  %s=%f\n", $c, $c;
1319
1320shows that the numeric value of $c follows changes to the values of $a
1321and $b.
1322
1323=head1 AUTHOR
1324
1325Ilya Zakharevich E<lt>F<ilya@math.mps.ohio-state.edu>E<gt>.
1326
1327=head1 DIAGNOSTICS
1328
1329When Perl is run with the B<-Do> switch or its equivalent, overloading
1330induces diagnostic messages.
1331
1332Using the C<m> command of Perl debugger (see L<perldebug>) one can
1333deduce which operations are overloaded (and which ancestor triggers
1334this overloading). Say, if C<eq> is overloaded, then the method C<(eq>
1335is shown by debugger. The method C<()> corresponds to the C<fallback>
1336key (in fact a presence of this method shows that this package has
1337overloading enabled, and it is what is used by the C<Overloaded>
1338function of module C<overload>).
1339
1340=head1 BUGS
1341
1342Because it is used for overloading, the per-package hash %OVERLOAD now
1343has a special meaning in Perl. The symbol table is filled with names
1344looking like line-noise.
1345
1346For the purpose of inheritance every overloaded package behaves as if
1347C<fallback> is present (possibly undefined). This may create
1348interesting effects if some package is not overloaded, but inherits
1349from two overloaded packages.
1350
1351Relation between overloading and tie()ing is broken.  Overloading is
1352triggered or not basing on the I<previous> class of tie()d value.
1353
1354This happens because the presence of overloading is checked too early,
1355before any tie()d access is attempted.  If the FETCH()ed class of the
1356tie()d value does not change, a simple workaround is to access the value
1357immediately after tie()ing, so that after this call the I<previous> class
1358coincides with the current one.
1359
1360B<Needed:> a way to fix this without a speed penalty.
1361
1362Barewords are not covered by overloaded string constants.
1363
1364This document is confusing.  There are grammos and misleading language
1365used in places.  It would seem a total rewrite is needed.
1366
1367=cut
1368
Note: See TracBrowser for help on using the repository browser.