in reply to Re^5: A little overloading conundrum
in thread A little overloading conundrum

Are there any caveats regarding the process by which this works ?

I think the main caveat is that if multiple classes do this, you have behaviour dependent on order of loading. It also becomes difficult or impossible for one such class to unwind its change (though it could test an "enabled" flag to make its own intervention a noop).

Replies are listed 'Best First'.
Re^7: A little overloading conundrum
by syphilis (Archbishop) on Mar 15, 2026 at 14:39 UTC
    Thanks for the reply. (I haven't been able to access this webshite for the last 3 days.)
    I was a bit confused by the fact that, with Choroba's script, the overloading was unaffected by the order in which package My::A and My::B were listed.
    Eventually, I deduced that the overriding "use overload" directives were, courtesy of being inside an eval block, not executed until after both packages had loaded.
    Anyway, I've been playing around with this approach and finding it to be relatively simple.

    I've decided that the implementation should be effected by a standalone module (Math::GMP_OLOAD) which will handle the Math::GMP side of things regarding the handling of Math::GMP objects in Math::MPFR, Math::GMPq and Math::GMPz. So it will contain stuff like:
    { package Math::GMP; use overload "+" => sub ($$$) { my($x, $y) = (shift, shift); if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) + eq 'Math::GMPz') { return $y + $x; } return Math::GMP::op_add($x, $y, 0); }; use overload "-" => sub ($$$) { my($x, $y) = (shift, shift); if(ref($y) eq 'Math::MPFR' || ref($y) eq 'Math::GMPq' || ref($y) + eq 'Math::GMPz') { return -($y - $x); } return Math::GMP::op_sub($x, $y, shift); }; ....
    Seems to be working ok.
    I doubt that anyone else will make much use of it, but I might use it a bit - and it's an enjoyable little exercise, anyway.
    It's nearly all done outside of the 3 modules it's supporting - ie it adds negligible clutter to those 3 modules.

    Cheers,
    Rob