in reply to overloading '0+'

The documentation of the module overload says:
# Transcendental functions

"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).

Overloading '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.

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
    So if we add function to handle the conversion to an integer there should be one call to truncate and other call to the overload integer function.
    #!/usr/bin/env perl # use strict; use warnings; package Soldier; use overload '0+' => \&truncate, 'int' => \&my_int; my $soldier1 = { NAME => 'BENJAMIN', RANK => 'PRIVATE', SERIAL => 151.11 }; bless $soldier1; print int($soldier1), "\n"; sub truncate { print "sub truncate called: ", $_[0]->{SERIAL}, "\n"; return $_[0]->{SERIAL}; } sub my_int { print "sub my_int called: ", $_[0]->{SERIAL}, "\n"; return int($_[0]->{SERIAL}); }
    Produces the (unexpected) output;
    sub my_int called: 151.11 151
    Thoughts?
      Your my_int-routine does not require its argument to be numified as it works directly on the object.

      If you force the object to be numified by changing the argument of int to: $soldier1 + 0 (you will have to overload '+' or add fallback => 1 to the overload hash) you will see truncate being called.

      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

Re^2: overloading '0+'
by syphilis (Archbishop) on Sep 03, 2007 at 02:47 UTC
    Thanks for all of the replies, guys.

    I had read the documentation (numerous times) but found it difficult to understand.

    Overloading '0+' is really overloading numification. So everytime you use $soldier1 in a context where it is used as a number, you call truncate ...

    That's the bit I was failing to understand. Things now start to fall into place.

    If I understand correctly, there's really no advantage in overloading '0+' unless one has a single subroutine that caters for a variety of overloadable numeric operations ?

    Cheers,
    Rob