📄 overload.3
字号:
.Ve.SpIf one or two of these operations are not overloaded, the remaining ones canbe used instead. \f(CW\*(C`bool\*(C'\fR is used in the flow control operators(like \f(CW\*(C`while\*(C'\fR) and for the ternary \f(CW\*(C`?:\*(C'\fR operation. These functions canreturn any arbitrary Perl value. If the corresponding operation for this valueis overloaded too, that operation will be called again with this value..SpAs a special case if the overload returns the object itself then it willbe used directly. An overloaded conversion returning the object isprobably a bug, because you're likely to get something that looks like\&\f(CW\*(C`YourPackage=HASH(0x8172b34)\*(C'\fR..IP "\(bu" 5\&\fIIteration\fR.Sp.Vb 1\& "<>".Ve.SpIf not overloaded, the argument will be converted to a filehandle orglob (which may require a stringification). The same overloadinghappens both for the \fIread-filehandle\fR syntax \f(CW\*(C`<$var>\*(C'\fR and\&\fIglobbing\fR syntax \f(CW\*(C`<${var}>\*(C'\fR..Sp\&\fB\s-1BUGS\s0\fR Even in list context, the iterator is currently called onlyonce and with scalar context..IP "\(bu" 5\&\fIDereferencing\fR.Sp.Vb 1\& \*(Aq${}\*(Aq, \*(Aq@{}\*(Aq, \*(Aq%{}\*(Aq, \*(Aq&{}\*(Aq, \*(Aq*{}\*(Aq..Ve.SpIf not overloaded, the argument will be dereferenced \fIas is\fR, thusshould be of correct type. These functions should return a referenceof correct type, or another object with overloaded dereferencing..SpAs a special case if the overload returns the object itself then itwill be used directly (provided it is the correct type)..SpThe dereference operators must be specified explicitly they will not be passed to\&\*(L"nomethod\*(R"..IP "\(bu" 5\&\fISpecial\fR.Sp.Vb 1\& "nomethod", "fallback", "=", "~~",.Ve.Spsee "\s-1SPECIAL\s0 \s-1SYMBOLS\s0 \s-1FOR\s0 \f(CW\*(C`use overload\*(C'\fR"..PPSee \*(L"Fallback\*(R" for an explanation of when a missing method can beautogenerated..PPA computer-readable form of the above table is available in the hash\&\f(CW%overload::ops\fR, with values being space-separated lists of names:.PP.Vb 10\& with_assign => \*(Aq+ \- * / % ** << >> x .\*(Aq,\& assign => \*(Aq+= \-= *= /= %= **= <<= >>= x= .=\*(Aq,\& num_comparison => \*(Aq< <= > >= == !=\*(Aq,\& \*(Aq3way_comparison\*(Aq=> \*(Aq<=> cmp\*(Aq,\& str_comparison => \*(Aqlt le gt ge eq ne\*(Aq,\& binary => \*(Aq& &= | |= ^ ^=\*(Aq,\& unary => \*(Aqneg ! ~\*(Aq,\& mutators => \*(Aq++ \-\-\*(Aq,\& func => \*(Aqatan2 cos sin exp abs log sqrt\*(Aq,\& conversion => \*(Aqbool "" 0+\*(Aq,\& iterators => \*(Aq<>\*(Aq,\& dereferencing => \*(Aq${} @{} %{} &{} *{}\*(Aq,\& special => \*(Aqnomethod fallback =\*(Aq.Ve.Sh "Inheritance and overloading".IX Subsection "Inheritance and overloading"Inheritance interacts with overloading in two ways..ie n .IP "Strings as values of ""use overload"" directive" 4.el .IP "Strings as values of \f(CWuse overload\fR directive" 4.IX Item "Strings as values of use overload directive"If \f(CW\*(C`value\*(C'\fR in.Sp.Vb 1\& use overload key => value;.Ve.Spis a string, it is interpreted as a method name..IP "Overloading of an operation is inherited by derived classes" 4.IX Item "Overloading of an operation is inherited by derived classes"Any class derived from an overloaded class is also overloaded. Theset of overloaded methods is the union of overloaded methods of allthe ancestors. If some method is overloaded in several ancestor, thenwhich description will be used is decided by the usual inheritancerules:.SpIf \f(CW\*(C`A\*(C'\fR inherits from \f(CW\*(C`B\*(C'\fR and \f(CW\*(C`C\*(C'\fR (in this order), \f(CW\*(C`B\*(C'\fR overloads\&\f(CW\*(C`+\*(C'\fR with \f(CW\*(C`\e&D::plus_sub\*(C'\fR, and \f(CW\*(C`C\*(C'\fR overloads \f(CW\*(C`+\*(C'\fR by \f(CW"plus_meth"\fR,then the subroutine \f(CW\*(C`D::plus_sub\*(C'\fR will be called to implementoperation \f(CW\*(C`+\*(C'\fR for an object in package \f(CW\*(C`A\*(C'\fR..PPNote that since the value of the \f(CW\*(C`fallback\*(C'\fR key is not a subroutine,its inheritance is not governed by the above rules. In the currentimplementation, the value of \f(CW\*(C`fallback\*(C'\fR in the first overloadedancestor is used, but this is accidental and subject to change..ie n .SH "SPECIAL SYMBOLS FOR ""use overload""".el .SH "SPECIAL SYMBOLS FOR \f(CWuse overload\fP".IX Header "SPECIAL SYMBOLS FOR use overload"Three keys are recognized by Perl that are not covered by the abovedescription..Sh "Last Resort".IX Subsection "Last Resort"\&\f(CW"nomethod"\fR should be followed by a reference to a function of fourparameters. If defined, it is called when the overloading mechanismcannot find a method for some operation. The first three arguments ofthis function coincide with the arguments for the corresponding method ifit were found, the fourth argument is the symbolcorresponding to the missing method. If several methods are tried,the last one is used. Say, \f(CW\*(C`1\-$a\*(C'\fR can be equivalent to.PP.Vb 1\& &nomethodMethod($a,1,1,"\-").Ve.PPif the pair \f(CW"nomethod" => "nomethodMethod"\fR was specified in the\&\f(CW\*(C`use overload\*(C'\fR directive..PPThe \f(CW"nomethod"\fR mechanism is \fInot\fR used for the dereference operators( ${} @{} %{} &{} *{} )..PPIf some operation cannot be resolved, and there is no functionassigned to \f(CW"nomethod"\fR, then an exception will be raised via \fIdie()\fR\-\-unless \f(CW"fallback"\fR was specified as a key in \f(CW\*(C`use overload\*(C'\fR directive..Sh "Fallback".IX Subsection "Fallback"The key \f(CW"fallback"\fR governs what to do if a method for a particularoperation is not found. Three different cases are possible depending onthe value of \f(CW"fallback"\fR:.IP "\(bu" 16\&\f(CW\*(C`undef\*(C'\fR.SpPerl tries to use asubstituted method (see \*(L"\s-1MAGIC\s0 \s-1AUTOGENERATION\s0\*(R"). If this fails, itthen tries to calls \f(CW"nomethod"\fR value; if missing, an exceptionwill be raised..IP "\(bu" 16\&\s-1TRUE\s0.SpThe same as for the \f(CW\*(C`undef\*(C'\fR value, but no exception is raised. Instead,it silently reverts to what it would have done were there no \f(CW\*(C`use overload\*(C'\fRpresent..IP "\(bu" 16defined, but \s-1FALSE\s0.SpNo autogeneration is tried. Perl tries to call\&\f(CW"nomethod"\fR value, and if this is missing, raises an exception..PP\&\fBNote.\fR \f(CW"fallback"\fR inheritance via \f(CW@ISA\fR is not carved in stoneyet, see \*(L"Inheritance and overloading\*(R"..Sh "Smart Match".IX Subsection "Smart Match"The key \f(CW"~~"\fR allows you to override the smart matching used bythe switch construct. See feature..Sh "Copy Constructor".IX Subsection "Copy Constructor"The value for \f(CW"="\fR is a reference to a function with threearguments, i.e., it looks like the other values in \f(CW\*(C`useoverload\*(C'\fR. However, it does not overload the Perl assignmentoperator. This would go against Camel hair..PPThis operation is called in the situations when a mutator is appliedto a reference that shares its object with some other reference, suchas.PP.Vb 2\& $a=$b;\& ++$a;.Ve.PPTo make this change \f(CW$a\fR and not change \f(CW$b\fR, a copy of \f(CW$$a\fR is made,and \f(CW$a\fR is assigned a reference to this new object. This operation isdone during execution of the \f(CW\*(C`++$a\*(C'\fR, and not during the assignment,(so before the increment \f(CW$$a\fR coincides with \f(CW$$b\fR). This is onlydone if \f(CW\*(C`++\*(C'\fR is expressed via a method for \f(CW\*(Aq++\*(Aq\fR or \f(CW\*(Aq+=\*(Aq\fR (or\&\f(CW\*(C`nomethod\*(C'\fR). Note that if this operation is expressed via \f(CW\*(Aq+\*(Aq\fRa nonmutator, i.e., as in.PP.Vb 2\& $a=$b;\& $a=$a+1;.Ve.PPthen \f(CW$a\fR does not reference a new copy of \f(CW$$a\fR, since $$a does notappear as lvalue when the above code is executed..PPIf the copy constructor is required during the execution of some mutator,but a method for \f(CW\*(Aq=\*(Aq\fR was not specified, it can be autogenerated as astring copy if the object is a plain scalar..IP "\fBExample\fR" 5.IX Item "Example"The actually executed code for.Sp.Vb 3\& $a=$b;\& Something else which does not modify $a or $b....\& ++$a;.Ve.Spmay be.Sp.Vb 4\& $a=$b;\& Something else which does not modify $a or $b....\& $a = $a\->clone(undef,"");\& $a\->incr(undef,"");.Ve.Spif \f(CW$b\fR was mathemagical, and \f(CW\*(Aq++\*(Aq\fR was overloaded with \f(CW\*(C`\e&incr\*(C'\fR,\&\f(CW\*(Aq=\*(Aq\fR was overloaded with \f(CW\*(C`\e&clone\*(C'\fR..PPSame behaviour is triggered by \f(CW\*(C`$b = $a++\*(C'\fR, which is consider a synonym for\&\f(CW\*(C`$b = $a; ++$a\*(C'\fR..SH "MAGIC AUTOGENERATION".IX Header "MAGIC AUTOGENERATION"If a method for an operation is not found, and the value for \f(CW"fallback"\fR is\&\s-1TRUE\s0 or undefined, Perl tries to autogenerate a substitute method forthe missing operation based on the defined operations. Autogenerated methodsubstitutions are possible for the following operations:.IP "\fIAssignment forms of arithmetic operations\fR" 16.IX Item "Assignment forms of arithmetic operations"\&\f(CW\*(C`$a+=$b\*(C'\fR can use the method for \f(CW"+"\fR if the method for \f(CW"+="\fRis not defined..IP "\fIConversion operations\fR" 16.IX Item "Conversion operations"String, numeric, and boolean conversion are calculated in terms of oneanother if not all of them are defined..IP "\fIIncrement and decrement\fR" 16.IX Item "Increment and decrement"The \f(CW\*(C`++$a\*(C'\fR operation can be expressed in terms of \f(CW\*(C`$a+=1\*(C'\fR or \f(CW\*(C`$a+1\*(C'\fR,and \f(CW\*(C`$a\-\-\*(C'\fR in terms of \f(CW\*(C`$a\-=1\*(C'\fR and \f(CW\*(C`$a\-1\*(C'\fR..ie n .IP """abs($a)""" 16.el .IP "\f(CWabs($a)\fR" 16.IX Item "abs($a)"can be expressed in terms of \f(CW\*(C`$a<0\*(C'\fR and \f(CW\*(C`\-$a\*(C'\fR (or \f(CW\*(C`0\-$a\*(C'\fR)..IP "\fIUnary minus\fR" 16.IX Item "Unary minus"can be expressed in terms of subtraction..IP "\fINegation\fR" 16.IX Item "Negation"\&\f(CW\*(C`!\*(C'\fR and \f(CW\*(C`not\*(C'\fR can be expressed in terms of boolean conversion, orstring or numerical conversion..IP "\fIConcatenation\fR" 16.IX Item "Concatenation"can be expressed in terms of string conversion..IP "\fIComparison operations\fR" 16.IX Item "Comparison operations"can be expressed in terms of its \*(L"spaceship\*(R" counterpart: either\&\f(CW\*(C`<=>\*(C'\fR or \f(CW\*(C`cmp\*(C'\fR:.Sp.Vb 2\& <, >, <=, >=, ==, != in terms of <=>\& lt, gt, le, ge, eq, ne in terms of cmp.Ve.IP "\fIIterator\fR" 16.IX Item "Iterator".Vb 1\& <> in terms of builtin operations.Ve.IP "\fIDereferencing\fR" 16.IX Item "Dereferencing".Vb 1\& ${} @{} %{} &{} *{} in terms of builtin operations.Ve.IP "\fICopy operator\fR" 16.IX Item "Copy operator"can be expressed in terms of an assignment to the dereferenced value, if thisvalue is a scalar and not a reference..SH "Minimal set of overloaded operations".IX Header "Minimal set of overloaded operations"Since some operations can be automatically generated from others, there isa minimal set of operations that need to be overloaded in order to havethe complete set of overloaded operations at one's disposal.Of course, the autogenerated operations may not do exactly what the userexpects. See \*(L"\s-1MAGIC\s0 \s-1AUTOGENERATION\s0\*(R" above. The minimal set is:.PP.Vb 4\& + \- * / % ** << >> x\& <=> cmp\& & | ^ ~\& atan2 cos sin exp log sqrt int.Ve.PPAdditionally, you need to define at least one of string, boolean ornumeric conversions because any one can be used to emulate the others.The string conversion can also be used to emulate concatenation..SH "Losing overloading".IX Header "Losing overloading"The restriction for the comparison operation is that even if, for example,`\f(CW\*(C`cmp\*(C'\fR' should return a blessed reference, the autogenerated `\f(CW\*(C`lt\*(C'\fR'function will produce only a standard logical value based on thenumerical value of the result of `\f(CW\*(C`cmp\*(C'\fR'. In particular, a workingnumeric conversion is needed in this case (possibly expressed in terms ofother conversions)..PPSimilarly, \f(CW\*(C`.=\*(C'\fR and \f(CW\*(C`x=\*(C'\fR operators lose their mathemagical propertiesif the string conversion substitution is applied..PPWhen you \fIchop()\fR a mathemagical object it is promoted to a string and itsmathemagical properties are lost. The same can happen with otheroperations as well..SH "Run-time Overloading".IX Header "Run-time Overloading"Since all \f(CW\*(C`use\*(C'\fR directives are executed at compile-time, the only way tochange overloading during run-time is to.PP.Vb 1\& eval \*(Aquse overload "+" => \e&addmethod\*(Aq;.Ve.PPYou can also use.PP.Vb 1\& eval \*(Aqno overload "+", "\-\-", "<="\*(Aq;.Ve.PPthough the use of these constructs during run-time is questionable..SH "Public functions".IX Header "Public functions"Package \f(CW\*(C`overload.pm\*(C'\fR provides the following public functions:.IP "overload::StrVal(arg)" 5.IX Item "overload::StrVal(arg)"Gives string value of \f(CW\*(C`arg\*(C'\fR as in absence of stringify overloading. If youare using this to get the address of a reference (useful for checking if tworeferences point to the same thing) then you may be better off using\&\f(CW\*(C`Scalar::Util::refaddr()\*(C'\fR, which is faster..IP "overload::Overloaded(arg)" 5.IX Item "overload::Overloaded(arg)"Returns true if \f(CW\*(C`arg\*(C'\fR is subject to overloading of some operations..IP "overload::Method(obj,op)" 5.IX Item "overload::Method(obj,op)"Returns \f(CW\*(C`undef\*(C'\fR or a reference to the method that implements \f(CW\*(C`op\*(C'\fR..SH "Overloading constants".IX Header "Overloading constants"For some applications, the Perl parser mangles constants too much.It is possible to hook into this process via \f(CW\*(C`overload::constant()\*(C'\fRand \f(CW\*(C`overload::remove_constant()\*(C'\fR functions..PPThese functions take a hash as an argument. The recognized keys of this hashare:.IP "integer" 8.IX Item "integer"to overload integer constants,.IP "float" 8.IX Item "float"to overload floating point constants,.IP "binary" 8.IX Item "binary"to overload octal and hexadecimal constants,.IP "q" 8.IX Item "q"to overload \f(CW\*(C`q\*(C'\fR\-quoted strings, constant pieces of \f(CW\*(C`qq\*(C'\fR\- and \f(CW\*(C`qx\*(C'\fR\-quotedstrings and here-documents,.IP "qr" 8.IX Item "qr"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -