Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: overloading '0+'

by blokhead (Monsignor)
on Sep 02, 2007 at 21:23 UTC ( [id://636638]=note: print w/replies, xml ) Need Help??


in reply to overloading '0+'

I don't know the answer to your first question.. But in general, the answer to your other questions is that when perl evaluates an overloaded object in some context (numeric, boolean, string, etc), or as an argument to certain core functions (abs, int, log, sqrt, etc), and there is no specific method for that context, it will "fall back" to other methods in order to find a reasonable value (see perldoc overload).

In the first example, there is no overload method defined for the int function. However, you have defined one for numeric context. So perl is smart enough to use that and pass the resulting number to the builtin int.

In the second example, you evaluate the object in boolean context, but no boolean overload method is defined. Perl has a standard way to evaluate numbers in boolean context, so it calls your numeric context method, and interprets the result as a boolean in the normal perl way.

Most of the time you want an overloaded object to be "consistent" with perl scalars in how they are evaluated in different contexts. So overload.pm is nice and lets you define the minimal number of overloaded methods, and the rest is handled automatically, following perl's rules for regular scalars.

blokhead

Log In?
Username:
Password:

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

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

    No recent polls found