- 论坛徽章:
- 46
|
回复 3# kelvenchi
我觉得你理解错了,手头没书不过重载有个 Magic Autogeneration 概念(详情看 perl overload),指的是定义了 <=> 可以不用定义 > < <= >= != 等这些逻辑上相关的操作符
Magic Autogeneration
If a method for an operation is not found then Perl tries to autogenerate a substitute implementation from the operations that have been defined.
Note: the behaviour described in this section can be disabled by setting fallback to FALSE (see fallback).
In the following tables, numbers indicate priority. For example, the table below states that, if no implementation for '!' has been defined then Perl will implement it using 'bool' (that is, by inverting the value returned by the method for 'bool' ); if boolean conversion is also unimplemented then Perl will use '0+' or, failing that, '""' .
operator | can be autogenerated from
|
| 0+ "" bool . x
=========|==========================
0+ | 1 2
"" | 1 2
bool | 1 2
int | 1 2 3
! | 2 3 1
qr | 2 1 3
. | 2 1 3
x | 2 1 3
.= | 3 2 4 1
x= | 3 2 4 1
<> | 2 1 3
-X | 2 1 3
Note: The iterator ('<>' ) and file test ('-X' ) operators work as normal: if the operand is not a blessed glob or IO reference then it is converted to a string (using the method for '""' , '0+' , or 'bool' ) to be interpreted as a glob or filename.
operator | can be autogenerated from
|
| < <=> neg -= -
=========|==========================
neg | 1
-= | 1
-- | 1 2
abs | a1 a2 b1 b2
< | 1
<= | 1
> | 1
>= | 1
== | 1
!= | 1
* one from [a1, a2] and one from [b1, b2]
Just as numeric comparisons can be autogenerated from the method for '<=>' , string comparisons can be autogenerated from that for 'cmp' :
operators | can be autogenerated from
====================|===========================
lt gt le ge eq ne | cmp
Similarly, autogeneration for keys '+=' and '++' is analogous to '-=' and '--' above:
operator | can be autogenerated from
|
| += +
=========|==========================
+= | 1
++ | 1 2
And other assignment variations are analogous to '+=' and '-=' (and similar to '.=' and 'x=' above):
operator || *= /= %= **= <<= >>= &= ^= |=
-------------------||--------------------------------
autogenerated from || * / % ** << >> & ^ |
Note also that the copy constructor (key '=' ) may be autogenerated, but only for objects based on scalars. See Copy Constructor. |
|