in reply to overloading '0+'
# Transcendental functionsOverloading '0+' is really overloading numification. So everytime you use $soldier1 in a context where it is used as a number, you call truncate and since as per the docs, int is autogenerated out of '0+' (unless you provide for its own routine), you get to call truncate twice: once to numify $soldier1 and once to take the integer value."atan2", "cos", "sin", "exp", "abs", "log", "sqrt", "int"
If abs is unavailable, it can be autogenerated using methods for "<" or "<=>" combined with either unary minus or subtraction.
Note that traditionally the Perl function int rounds to 0, thus for floating-point-like types one should follow the same semantic. If int is unavailable, it can be autogenerated using the overloading of 0+.
# Boolean, string and numeric conversion
'bool', '""', '0+',
If one or two of these operations are not overloaded, 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.
As a special case if the overload returns the object itself then it will be used directly. An overloaded conversion returning the object is probably a bug, because you're likely to get something that looks like YourPackage=HASH(0x8172b34).
You therefore really should provide a separate definition for the numification (returning the SERIAL number) and then "overload" int
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: overloading '0+'
by bruceb3 (Pilgrim) on Sep 03, 2007 at 05:26 UTC | |
by CountZero (Bishop) on Sep 03, 2007 at 09:11 UTC | |
Re^2: overloading '0+'
by syphilis (Archbishop) on Sep 03, 2007 at 02:47 UTC |