Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: A little overloading conundrum

by syphilis (Archbishop)
on Mar 07, 2026 at 14:26 UTC ( [id://11167415]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: A little overloading conundrum
by ysth (Canon) on Mar 08, 2026 at 03:22 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11167415]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2026-04-21 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.