Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Overloading oddity

by BrowserUk (Patriarch)
on Jan 27, 2004 at 22:33 UTC ( [id://324552]=note: print w/replies, xml ) Need Help??


in reply to Overloading oddity

The problem is that your overload function does not take into account the differences when being called as a conventional method, and when it is called via the overloading. In the latter case, there is an extra 3rd parameter identifying the ordering of the operands for the operator.

From the overload pod:

Calling Conventions for Binary Operations The functions specified in the use overload ... directive are called with three (in one particular case with four, see Last Resort) arguments. If the corresponding operation is binary, then the first two arguments are the two arguments of the operation. However, due to general object calling conventions, the first argument should always be an object in the package, so in the situation of 7+$a, the order of the arguments is interchanged. It probably does not matter when implementing the addition method, but whether the arguments are reversed is vital to the subtraction method. The method can query this information by examining the third argument, which can take three different values: FALSE - the order of arguments is as in the current operation. TRUE - the arguments are reversed. undef - the current operation is an assignment variant (as in $a+=7), but the usual function is called instead.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: Re: Overloading oddity
by theguvnor (Chaplain) on Jan 27, 2004 at 23:01 UTC
    Thanks BrowserUK - I read perldoc overload but didn't recognize that this was the problem. I guess the only decent solution is to provide the stringify() function explicitly that returns the name without the extra functions in the name() mutator method.

    [Jon]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found