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