I expect (but am not certain) that the '0+' overload is also invoked if an XSUB attempts to convert an object to an IV or UV
No, I'm quite certain that doesn't happen. (And if I'm wrong about that, then I'm also now quite certain that I'll be told about it ;-)
It's just that the '0+' overloading calls an XSUB that converts the value in the object to a signed/unsigned long and returns that signed/unsigned long as an IV/UV.
Of course, you can always call intify or uintify (though the Math::GMP docs recommend avoiding the latter) without invoking any overloading.
Still not sure if calling
int($obj) is the only way to activate the overload '0+' sub, but it's the only way that I could find.
There's probably good lessons to be learned from working through this, but I think I'll give it a miss for now, mainly because I don't see much value in what this Math::GMP overloading of '0+' provides.
Interesting stuff, but.
UPDATE: I took a look at Math::BigInt, and found that the '0+' overloading allows stuff like:
C:\>perl -MMath::GMP -wle "@x = qw(a b c d e f g h i j k l m n); print
+ $x[Math::GMP->new(-2)];"
m
Can do the same with Math::GMPz ... not exactly sure how it works there (as there's no explicit '0+' overloading) :
C:\>perl -MMath::GMPz -wle "@x = ('a' .. 'n'); for(Math::GMPz->new(0)
+.. Math::GMPz->new(@x - 1)) { print $x[$_] }"
a
b
c
d
e
f
g
h
i
j
k
l
m
n
UPDATE 2: This capability of '0+' overloading definitely provides a useful feature.
And then
Haarg has also
pointed out that it contributes to sprintf() and pack() functionality - which adds to its usefulness.
Cheers,
Rob
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.