home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / perl / !Perl / Manual / perlovl < prev    next >
Encoding:
Text File  |  1995-04-18  |  13.8 KB  |  350 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLOVL </TITLE>
  5. <h2>NAME </h2>
  6. perlovl - perl overloading semantics
  7. <p><h2>SYNOPSIS</h2>
  8. <pre>
  9.         package SomeThing;
  10. </pre>
  11. <pre>
  12.         %OVERLOAD = (
  13.         '+' => \&myadd,
  14.         '-' => \&mysub,
  15.         # etc
  16.         );
  17.         ...
  18. </pre>
  19. <pre>
  20.         package main;
  21.         $a = new SomeThing 57;
  22.         $b=5+$a;
  23. </pre>
  24. <h2>CAVEAT SCRIPTOR</h2>
  25. Overloading of operators is a subject not to be taken lightly.
  26. Neither its precise implementation, syntax, nor semantics are
  27. 100% endorsed by Larry Wall.  So any of these may be changed 
  28. at some point in the future.
  29. <p><h2>DESCRIPTION</h2>
  30. <h3>Declaration of overloaded functions</h3>
  31. <pre>
  32.         package Number;
  33.         %OVERLOAD = ( 
  34.         "+" => \&add, 
  35.         "*=" => "muas"
  36.         );
  37. </pre>
  38. declares function Number::add() for addition, and method muas() in
  39. the "class" <B>Number</B> (or one of its base classes)
  40. for the assignment form <B>*=</B> of multiplication.  Legal values of this
  41. hash array are values legal inside <B>&{ ... }</B> call, so the name of a
  42. subroutine, a reference to a subroutine, or an anonymous subroutine 
  43. will all work.
  44. <p>The subroutine <B>$OVERLOAD{"+"}</B> will be called to execute <B>$a+$b</B> if $a
  45. is a reference to an object blessed into the package <B>Number</B>, or $a is
  46. not an object from a package with defined mathemagic addition, but $b is a
  47. reference to a <B>Number</B>.  It can be called also in other situations, like
  48. <B>$a+=7</B>, or <B>$a++</B>.  See MAGIC AUTOGENERATION.  (Mathemagical
  49. methods refer to methods triggered by an overloaded mathematical
  50. operator.)
  51. <p><h3>Calling Conventions for Binary Operations</h3>
  52. The functions in <B>values %OVERLOAD</B> are called with three (in one
  53. particular case with four, see Last Resort) arguments.  If the
  54. corresponding operation is binary, then the first two arguments are the
  55. two arguments of the operation.  However, due to general object calling
  56. conventions, the first argument should be always an object in the package,
  57. so in the situation of <B>7+$a</B>, the order of arguments is interchanged.
  58. Most probably it does not matter for implementation of the addition
  59. method, but whether the arguments are reversed is vital for the
  60. subtraction method.  The subroutine can query this information by
  61. examining the third argument, which can take three different values:
  62. <p>
  63. <dl>
  64. <dt><B>FALSE</B>
  65. <dd>
  66. the order of arguments is as in the current operation.
  67. <p></dd>
  68. <dt><B>TRUE</B>
  69. <dd>
  70. the arguments are reversed.
  71. <p></dd>
  72. <dt><B><A HREF="perlfunc.html#perlfunc_258">undef</A> </B>
  73. <dd>
  74. the current operation is an assignment variant (as in
  75. <B>$a+=7</B>), but the usual function is called instead.  This additional
  76. information can be used to generate some optimizations.
  77. <p></dd>
  78.  
  79. </dl>
  80.  
  81. <h3>Calling Conventions for Unary Operations</h3>
  82. Unary operation are considered binary operations with the second
  83. argument being 
  84. <A HREF="perlfunc.html#perlfunc_258">undef</A>
  85. .  Thus <B>$OVERLOAD{"++"}</B> is called with
  86. arguments <B>($a,undef,'')</B> when $a++ is executed.
  87. <p><h3>Overloadable Operations</h3>
  88. The following keys of %OVERLOAD are recognized:
  89. <p>
  90. <dl>
  91.  
  92. <dt><b><A NAME="perlcall_39">* <I>Arithmetic operations</I></A></b>
  93. <pre>
  94.         "+", "+=", "-", "-=", "*", "*=", "/", "/=", "%", "%=",
  95.         "**", "**=", "<<", "<<=", >>", >>=", "x", "x=", ".", ".=",
  96. </pre>
  97. <dd>
  98. For these operations a substituted non-assignment variant can be called if
  99. the assignment variant is not available.  Methods for operations "<B>+</B>",
  100. "<B>-</B>", "<B>+=</B>", and "<B>-=</B>" can be called to automatically generate
  101. increment and decrement methods.  The operations "<B>-</B>" can be used to
  102. autogenerate missing methods for unary minus or 
  103. <A HREF="perlfunc.html#perlfunc_72">abs</A>
  104. .
  105. <p></dd>
  106. <dt><B>* <I>Comparison operations</I></B>
  107. <pre>
  108.         "<",  "<=", ">",  ">=", "==", "!=", "<=>",
  109.         "lt", "le", "gt", "ge", "eq", "ne", "cmp",
  110. </pre>
  111. <dd>
  112. If the corresponding "spaceship" variant is available, it can be
  113. used to substitute for the missing operation.  During 
  114. <A HREF="perlfunc.html#perlfunc_234">sort</A>
  115. ing
  116. arrays, <B>cmp</B> is used to compare values subject to %OVERLOAD.
  117. <p></dd>
  118. <dt><B>* <I>Bit operations</I></B>
  119. <pre>
  120.         "&", "^", "|", "neg", "!", "~",
  121. </pre>
  122. <dd>
  123. "<B>neg</B>" stands for unary minus.  If the method for <B>neg</B> is not
  124. specified, it can be autogenerated using on the method for subtraction.
  125. <p></dd>
  126. <dt><B>* <I>Increment and decrement</I></B>
  127. <pre>
  128.         "++", "--",
  129. </pre>
  130. <dd>
  131. If undefined, addition and subtraction methods can be
  132. used instead.  These operations are called both in prefix and
  133. postfix form.
  134. <p></dd>
  135. <dt><B>* <I>Transcendental functions</I></B>
  136. <pre>
  137.         "atan2", "cos", "sin", "exp", "abs", "log", "sqrt",
  138. </pre>
  139. <dd>
  140. If 
  141. <A HREF="perlfunc.html#perlfunc_72">abs</A>
  142.  is unavailable, it can be autogenerated using methods
  143. for "<" or "<=>" combined with either unary minus or subtraction.
  144. <p></dd>
  145. <dt><B>* <I>Boolean, string and numeric conversion</I></B>
  146. <pre>
  147.         "bool", "\"\"", "0+",
  148. </pre>
  149. <dd>
  150. If one or two of these operations are unavailable, the remaining ones can
  151. be used instead.  <B>bool</B> is used in the flow control operators
  152. (like <B>while</B>) and for the ternary "<B>?:</B>" operation.  These functions can
  153. return any arbitrary Perl value.  If the corresponding operation for this value
  154. is overloaded too, that operation will be called again with this value.
  155. <p></dd>
  156. <dt><B>* <I>Special</I></B>
  157. <pre>
  158.         "nomethod", "fallback", "=",
  159. </pre>
  160. <dd>
  161. see SPECIAL KEYS OF %OVERLOAD.
  162. <p></dd>
  163.  
  164. </dl>
  165.  
  166. See "Fallback" for an explanation of when a missing method can be autogenerated.
  167. <p><h2>SPECIAL KEYS OF %OVERLOAD</h2>
  168. Three keys are recognized by Perl that are not covered by the above
  169. description.
  170. <p><h3>Last Resort</h3>
  171. <B>$OVERLOAD{"nomethod"}</B> is a reference to a function of four parameters.
  172. If defined, it is called when the overloading mechanism cannot find a
  173. method for some operation.  The first three arguments of this function
  174. coincide with arguments for the corresponding method if it were found, the
  175. fourth argument is the key of %OVERLOAD corresponding to the missing
  176. method.  If several methods are tried, the last one is used.  Say, <B>1-$a</B>
  177. can be equivalent to
  178. <p><pre>
  179.         &{ $Pack::OVERLOAD{"nomethod"} }($a,1,1,"-").
  180. </pre>
  181. If some operation cannot be resolved, and there is no
  182. <B>$OVERLOAD{"nomethod"}</B>, then an exception will be raised 
  183. via die() -- unless <B>$OVERLOAD{"fallback"}</B> is true. 
  184. <p><h3>Fallback </h3>
  185. <B>$OVERLOAD{"fallback"}</B> governs what to do if a method for a particular
  186. operation is not found.  Three different cases are possible depending on
  187. value of <B>$OVERLOAD{"fallback"}</B>:
  188. <p>
  189. <dl>
  190. <dt><B>*  <A HREF="perlfunc.html#perlfunc_258">undef</A> </B>
  191. <dd>
  192. Perl tries to use a
  193. substituted method (see MAGIC AUTOGENERATION).  If this fails, it
  194. then tries to calls <B>$OVERLOAD{"nomethod"}</B>; if missing, an exception
  195. will be raised.
  196. <p></dd>
  197. <dt><B>* TRUE</B>
  198. <dd>
  199. The same as for the 
  200. <A HREF="perlfunc.html#perlfunc_258">undef</A>
  201.  value, but no exception is raised.  Instead,
  202. it silently reverts to what it would have done were there no %OVERLOAD is
  203. present.
  204. <p></dd>
  205. <dt><B>* defined, but FALSE</B>
  206. <dd>
  207. No autogeneration is tried.  Perl tries to call
  208. <B>$OVERLOAD{"nomethod"}</B>, and if this is missing, raises an exception. 
  209. <p></dd>
  210.  
  211. </dl>
  212.  
  213. <h3>Copy Constructor</h3>
  214. <B>$OVERLOAD{"="}</B> is a reference to a function with three arguments,
  215. i.e., it looks like a usual value of %OVERLOAD.  What is special about
  216. this subroutine is that it should not return a blessed reference into
  217. a package (as most other methods are expected to), but rather a freshly made
  218. copy of its dereferenced argument (see "BUGS", though).  This operation
  219. is called in the situations when a mutator is applied to a reference
  220. that shares its object with some other reference, such as
  221. <p><pre>
  222.         $a=$b; 
  223.         $a++;
  224. </pre>
  225. To make this change to $a and not to change $b, a freshly made copy of
  226. <B>$$a</B> is made, and $a is assigned a reference to this object.  This
  227. operation is executed during <B>$a++</B>, (so before this <B>$$a</B> coincides
  228. with <B>$$b</B>), and only if <B>++</B> is expressed via  <B>$OPERATOR{'++'}</B> or
  229. <B>$OPERATOR{'+='}</B>.  Note that if this operation is expressed via '<B>+</B>',
  230. i.e., as
  231. <p><pre>
  232.         $a=$b; 
  233.         $a=$a+1;
  234. </pre>
  235. then <B>$$a</B> and <B>$$b</B> do not appear as lvalues.
  236. <p>If the copy constructor is required during execution of some mutator, but
  237. <B>$OPERATOR{'='}</B> is missing, it can be autogenerated as a string
  238. copy if an object of
  239. the package is a plain scalar.
  240. <p><h2>MAGIC AUTOGENERATION</h2>
  241. If a method for an operation is not found, and <B>$OVERLOAD{"fallback"}</B> is
  242. TRUE or undefined, Perl tries to to autogenerate a substitute method for
  243. the missing operation based on defined operations.  Autogenerated method
  244. substitutions are possible for the following operations:
  245. <p>
  246. <dl>
  247. <dt><B><I>Assignment forms of arithmetic operations</I></B>
  248. <dd>
  249. <B>$a=+$b</B> can use the <B>$OVERLOAD{"+"}</B> method if <B>$OVERLOAD{"+="}</B>
  250. is not defined.
  251. <p></dd>
  252. <dt><B><I>Conversion operations</I> </B>
  253. <dd>
  254. String, numeric, and boolean conversion are calculated in terms of one
  255. another if not all of them are defined.
  256. <p></dd>
  257. <dt><B><I>Increment and decrement</I></B>
  258. <dd>
  259. The <B>++$a</B> operation can be expressed in terms of <B>$a+=1</B> or <B>$a+1</B>,
  260. and <B>$a--</B> in terms of <B>$a-=1</B> and <B>$a-1</B>.
  261. <p></dd>
  262. <dt><B><B>abs($a)</B></B>
  263. <dd>
  264. can be expressed in terms of <B>$a<0</B> and <B>-$a</B> (or <B>0-$a</B>).
  265. <p></dd>
  266. <dt><B><I>Unary minus</I></B>
  267. <dd>
  268. can be expressed in terms of subtraction.
  269. <p></dd>
  270. <dt><B><I>Concatenation</I></B>
  271. <dd>
  272. can be expressed in terms of string conversion.
  273. <p></dd>
  274. <dt><B><I>Comparison operations</I> </B>
  275. <dd>
  276. can be expressed in terms of its "spaceship" counterpart: either
  277. <B><=></B> or <B>cmp</B>:
  278.  
  279.     <, >, <=, >=, ==, !=     in terms of <=>
  280.     lt, gt, le, ge, eq, ne     in terms of cmp
  281. <p></dd>
  282. <dt><B><I>Copy operator</I></B>
  283. <dd>
  284. can be expressed in terms of assignment to the dereferenced value, if this
  285. value is scalar but not a reference.
  286. <p></dd>
  287.  
  288. </dl>
  289.  
  290. <h2>WARNING</h2>
  291. The restriction for the comparison operation is that even if, for example,
  292. `<B>cmp</B>' should return a blessed reference, the autogenerated `<B>lt</B>'
  293. function will produce only a standard logical value based on the
  294. numerical value of the result of `<B>cmp</B>'.  In particular, a working
  295. numeric conversion is needed in this case (possibly expressed in terms of
  296. other conversions).
  297. <p>Similarly, <B>.=</B>  and <B>x=</B> operators lose their mathemagical properties
  298. if the string conversion substitution is applied.
  299. <p>When you chop() a mathemagical object, it becomes promoted to a string
  300. first, and its mathemagical qualities is lost.  The same can happen with other
  301. operations as well.
  302. <p><h2>IMPLEMENTATION</h2>
  303. The table of methods for all operations is cached as a magic for the
  304. symbol table hash of the package.  It is rechecked for changes of
  305. %OVERLOAD and @ISA only during 
  306. <A HREF="perlfunc.html#perlfunc_78">bless</A>
  307. ing; so if it is changed
  308. dynamically, you'll need an additional fake 
  309. <A HREF="perlfunc.html#perlfunc_78">bless</A>
  310. ing to update the
  311. table.
  312. <p>(Every SVish thing has a magic queue, and a magic is an entry in that queue.
  313. This is how a single variable may participate in multiple forms of magic
  314. simultaneously.  For instance, environment variables regularly have two
  315. forms at once: their %ENV magic and their taint magic.)
  316. <p>If an object belongs to a package with %OVERLOAD, it carries a special
  317. flag.  Thus the only speed penalty during arithmetic operations without
  318. overload is the check of this flag.
  319. <p>In fact, if no %OVERLOAD is ever accessed, there is almost no overhead for
  320. overloadable operations, so most programs should not suffer measurable
  321. performance penalties.  Considerable effort was made minimize overhead
  322. when %OVERLOAD is accessed and the current operation is overloadable but
  323. the arguments in question do not belong to packages with %OVERLOAD.  When
  324. in doubt, test your speed with %OVERLOAD and without it.  So far there
  325. have been no reports of substantial speed degradation if Perl is compiled
  326. with optimization turned on.
  327. <p>There is no size penalty for data if there is no %OVERLOAD. 
  328. <p>The copying like <B>$a=$b</B> is shallow; however, a one-level-deep
  329. copying is 
  330. carried out before any operation that can imply an assignment to the
  331. object $b (or $a) refers to, like <B>$b++</B>.  You can override this
  332. behavior by defining your copy constructor (see "Copy Constructor").
  333. <p>It is expected that arguments to methods that are not explicitly supposed
  334. to be changed are constant (but this is not enforced).
  335. <p><h2>AUTHOR</h2>
  336. Ilya Zakharevich <<I>ilya@math.mps.ohio-state.edu</I>>.
  337. <p><h2>DIAGNOSTICS</h2>
  338. When Perl is run with the <B>-Do</B> switch or its equivalent, overloading
  339. induces diagnostic messages.
  340. <p><h2>BUGS</h2>
  341. Because it's used for overloading, the per-package associative array
  342. %OVERLOAD now has a special meaning in Perl.
  343. <p>Although the copy constructor is specially designed to make overloading
  344. operations with references to an array simpler, as it now works it's
  345. useless for this because a subroutine cannot return an array in the same
  346. way as it returns a scalar (from the point of view of Perl
  347. internals).  Expect a change of interface for the copy constructor.
  348. <p>As shipped, %OVERLOAD is not inherited via the @ISA tree.  A patch for
  349. this is available from the author.
  350. <p>This document is confusing.<p>