Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^6: How does Math::GMP overload the assignment operator ? (more guessing)

by syphilis (Archbishop)
on Nov 10, 2020 at 11:52 UTC ( [id://11123556]=note: print w/replies, xml ) Need Help??


in reply to Re^5: How does Math::GMP overload the assignment operator ? (more guessing)
in thread How does Math::GMP overload the assignment operator ?

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

Replies are listed 'Best First'.
Re^7: How does Math::GMP overload the assignment operator ? (more guessing)
by Haarg (Priest) on Nov 10, 2020 at 19:54 UTC
    sprintf and pack can trigger the 0+ overload, even if all other ops are overloaded.
      sprintf and pack can trigger the 0+ overload

      Now that you mention it, I do recall experiencing that overloading with sprintf(), but I failed to recognize it for what it was.
      "How the hell does that work ?", I thought ... and then quickly replied "Gee, that's handy !" ... whereupon that particular conversation with myself was abruptly ended.
      For anyone seeking an example:
      C:\>perl -MMath::GMP -le "printf '%x', Math::GMP->new(10);" a C:\>perl -MMath::GMP -le "print unpack 'H*', pack 'I>', Math::GMP->new +(254);" 000000fe
      Cheers,
      Rob
Re^7: How does Math::GMP overload the assignment operator ? (more guessing)
by jcb (Parson) on Nov 11, 2020 at 00:52 UTC

    Is there a string conversion overload in Math::GMPz? Perl has quite a few ways to construct overloaded operators from other operators, and one possibility is to first convert the object to a string and then use the builtin string->number conversion.

      one possibility is to first convert the object to a string and then use the builtin string->number conversion

      Yes, that seems consistent with the behaviour I've noticed.
      In the absence of a specified '0+' overload subroutine, $array[Math::GMPz->new(-2)] would be stringified to $array["-2"] and then numified to $array[-2]

      Cheers,
      Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-25 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found