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


In reply to Re: overloading '0+' by blokhead
in thread overloading '0+' by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.