package SomeThing;
use overload '+' => \&myadd, '-' => \&mysub; # etc ...
package main; $a = new SomeThing 57; $b=5+$a; ... if (overload::Overloaded $b) {...} ... $strval = overload::StrVal $b;
package Number; use overload "+" => \&add, "*=" => "muas";declares function Number::add() for addition, and method muas() in the ``class'' Number (or one of its base classes) for the assignment form *= of multiplication.
Arguments of this directive come in (key, value) pairs. Legal values are values legal inside a &{ ... } call, so the name of a subroutine, a reference to a subroutine, or an anonymous subroutine will all work. Legal keys are listed below.
The subroutine add will be called to execute $a+$b if $a is a reference to an object blessed into the package Number, or if $a is not an object from a package with defined mathemagic addition, but $b is a reference to a Number. It can also be called in other situations, like $a+=7, or $a++. See the section on MAGIC AUTOGENERATION. (Mathemagical methods refer to methods triggered by an overloaded mathematical operator.)
"+", "+=", "-", "-=", "*", "*=", "/", "/=", "%", "%=", "**", "**=", "<<", "<<=", ">>", ">>=", "x", "x=", ".", ".=",For these operations a substituted non-assignment variant can be called if the assignment variant is not available. Methods for operations ``+'', ``-'', ``+='', and ``-='' can be called to automatically generate increment and decrement methods. The operation ``-'' can be used to autogenerate missing methods for unary minus or abs.
"<", "<=", ">", ">=", "==", "!=", "<=>", "lt", "le", "gt", "ge", "eq", "ne", "cmp",If the corresponding ``spaceship'' variant is available, it can be used to substitute for the missing operation. During sorting arrays, cmp is used to compare values subject to use overload.
"&", "^", "|", "neg", "!", "~",``neg'' stands for unary minus. If the method for neg is not specified, it can be autogenerated using the method for subtraction.
"++", "--",If undefined, addition and subtraction methods can be used instead. These operations are called both in prefix and postfix form.
"atan2", "cos", "sin", "exp", "abs", "log", "sqrt",If abs is unavailable, it can be autogenerated using methods for ``<'' or ``<=>'' combined with either unary minus or subtraction.
"bool", "\"\"", "0+",If one or two of these operations are unavailable, the remaining ones can be used instead. bool is used in the flow control operators (like while) and for the ternary ``?:'' operation. These functions can return any arbitrary Perl value. If the corresponding operation for this value is overloaded too, that operation will be called again with this value.
"nomethod", "fallback", "=",see the section on SPECIAL SYMBOLS FOR use overload.
See the section on Fallback for an explanation of when a missing method can be autogenerated.
&nomethodMethod($a,1,1,"-")if the pair "nomethod" => "nomethodMethod" was specified in the use overload directive.
If some operation cannot be resolved, and there is no function assigned to "nomethod", then an exception will be raised via die()-- unless "fallback" was specified as a key in use overload directive.
This operation is called in the situations when a mutator is applied to a reference that shares its object with some other reference, such as
$a=$b; $a++;To make this change $a and not change $b, a copy of $$a is made, and $a is assigned a reference to this new object. This operation is done during execution of the $a++, and not during the assignment, (so before the increment $$a coincides with $$b). This is only done if ++ is expressed via a method for '++' or '+='. Note that if this operation is expressed via '+' a nonmutator, i.e., as in
$a=$b; $a=$a+1;then $a does not reference a new copy of $$a, since $$a does not appear as lvalue when the above code is executed.
If the copy constructor is required during the execution of some mutator,
but a method for '=' was not specified, it can be autogenerated as a
string copy if the object is a plain scalar.
$a=$b; Something else which does not modify $a or $b.... ++$a;may be
$a=$b; Something else which does not modify $a or $b.... $a = $a->clone(undef,""); $a->incr(undef,"");if $b was mathemagical, and '++' was overloaded with \&incr, '=' was overloaded with \&clone.
Similarly, .= and x= operators lose their mathemagical properties if the string conversion substitution is applied.
When you chop() a mathemagical object it is promoted to a string and its mathemagical properties are lost. The same can happen with other operations as well.
eval 'use overload "+" => \&addmethod';You can also use
eval 'no overload "+", "--", "<="';though the use of these constructs during run-time is questionable.
The table of methods for all operations is cached as magic in the symbol table hash for the package. The table is rechecked for changes due to use overload, no overload, and @ISA only during blessing; so if they are changed dynamically, you'll need an additional fake blessing to update the table.
(Every SVish thing has a magic queue, and magic is an entry in that queue. This is how a single variable may participate in multiple forms of magic simultaneously. For instance, environment variables regularly have two forms at once: their %ENV magic and their taint magic.)
If an object belongs to a package using overload, it carries a special flag. Thus the only speed penalty during arithmetic operations without overloading is the checking of this flag.
In fact, if use overload is not present, there is almost no overhead for overloadable operations, so most programs should not suffer measurable performance penalties. A considerable effort was made to minimize the overhead when overload is used and the current operation is overloadable but the arguments in question do not belong to packages using overload. When in doubt, test your speed with use overload and without it. So far there have been no reports of substantial speed degradation if Perl is compiled with optimization turned on.
There is no size penalty for data if overload is not used.
Copying ($a=$b) is shallow; however, a one-level-deep copying is carried out before any operation that can imply an assignment to the object $a (or $b) refers to, like $a++. You can override this behavior by defining your own copy constructor (see the section on Copy Constructor).
It is expected that arguments to methods that are not explicitly supposed to be changed are constant (but this is not enforced).
As shipped, mathemagical properties are not inherited via the @ISA tree.
This document is confusing.