Hi,
I've read the overload documentation and I understand the basics of overloading the '=' operator.
I also understand that it's not really an overloading of the '=' operator - rather there's a process, that begins with doing
$copy = $orig, whereby $copy ultimately becomes a separate copy of the Math::GMP object
$orig just prior to taking on a new value.
In Math::GMP this can be demonstrated as:
use strict;
use warnings;
use Math::GMP;
my $orig = Math::GMP->new(2);
my $copy = $orig;
# At this point $copy and $orig refer to
# one and the same Math::GMP object.
$copy += 5;
# At this point, $copy refers to a newly
# created Math::GMP object that holds a
# value of 7; $orig still refers to the
# original object, holding a value of 2.
print "$orig\n"; # prints 2
print "$copy\n"; # prints 7
I have implemented the exact same thing in my Math::GMPz module - just replace all occurrences of "GMP" with "GMPz" and that little demo will behave in exactly the same way.
But now to the bit that I don't understand:
In Math::GMPz that overloading only works because, in GMPz.pm, I've done:
use overload
... ,
'=' => \&overload_copy,
... ;
If I remove that key-value pair from GMPz.pm then that particular overloading ceases to work. In that demo, both $copy and $orig will then end up referring to the very same (original) object, but with the new value of 7 - and the script crashes on termination.
OTOH, GMP.pm does not supply overload.pm with an '=', sub{} key-value pair at all.
How come this overloading still works for Math::GMP ?
Cheers,
Rob