in reply to A little overloading conundrum

There's nothing I can think of and the documentation suggests it's not something that is easy to do.

One could reverse the order so $A_obj is checked first: my $n2 = -$A_obj + $B_obj;. That's obviously not practical, though, and would also need neg overloading on $A_obj (and addition if not already done).

Replies are listed 'Best First'.
Re^2: A little overloading conundrum
by syphilis (Archbishop) on Mar 07, 2026 at 14:26 UTC
    There's nothing I can think of and the documentation suggests it's not something that is easy to do.

    Yes - the first point I read in that link to the documentation pretty much kills all hope:
    1.If the first operand has declared a subroutine to overload the opera +tor then use that implementation.
    I suppose (untested) I could do:
    my $n2 = (\$B_obj) - $A_obj;
    and that would at least call module A's oload_minus() subroutine .... which would then be structured to de-reference the first argument and return the intended result.
    But that solution is no more practical than the alternative you provided.

    Thanks for the reply. I had, of course, consulted the overloading docs but had stopped reading before reaching the bit to which you linked.
    It's not the end of the world if I have to modify module B.

    Cheers,
    Rob
      If you are working around it in the caller, this is clearer, if not as efficient:
      my $n2 = -($A_obj - $B_obj);
        If you are working around it in the caller...

        Apologies - I should have explicitly stated that "working around it in the caller" is not a solution.
        It's imperative that, wrt the given pseudo-example, my $n2 = $B_obj - $A_obj; return a module A object with a value of -10.
        Hence my agreement with sw1's labelling of both his and my alterations to the caller as being "not practical".

        If module B did not overload the '-' operator then, AIUI, my $n2 = $B_obj - $A_obj; would invoke module A's overloading of that operator - which is exactly what I want.
        I was hoping there might have been some way of triggering that same behaviour when module B does overload that operator .... but it seems that's not the case.

        Cheers,
        Rob