Just another Perl shrine | |
PerlMonks |
How does Math::GMP overload the assignment operator ?by syphilis (Archbishop) |
on Nov 07, 2020 at 04:36 UTC ( [id://11123464]=perlquestion: print w/replies, xml ) | Need Help?? |
syphilis has asked for the wisdom of the Perl Monks concerning the following question:
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: 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:
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
Back to
Seekers of Perl Wisdom
|
|