Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: How does Math::GMP overload the assignment operator ?

by karlgoethebier (Abbot)
on Nov 10, 2020 at 16:55 UTC ( #11123557=note: print w/replies, xml ) Need Help??


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

«The real solution»

Yet another ToE or another Promised Land? It would be nice if you could provide a simple example for the uninitiated. No kidding. Best regards.

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re^7: How does Math::GMP overload the assignment operator ?
by syphilis (Bishop) on Nov 11, 2020 at 00:31 UTC
    It would be nice if you could provide a simple example for the uninitiated

    The problem was raised back in11123482. It's essentially demonstrated by the fact that the following 2 demos do different things - which means that one of them doesn't DWIM.
    use Math::GMP; $orig = Math::GMP->new(5); $copy = $orig; $copy += 4; # $copy is 9 # $orig is 4
    VERSUS:
    use Math::GMP; $orig = Math::GMP->new(5); $copy = $orig; $copy->add_ui_gmp(4); # $copy is 9 # $orig is 9
    Haarg was saying that you avoid this issue by not providing the add_ui_gmp() method in the first place.
    It's not needed because you can simply use the operator overloading (as per the first example) to achieve the result you want.
    Furthermore, you would not provide every other method that offers in-place altering of values - thereby forcing the user to utilize the (existing) operator overloading that performs the same task.
    This approach requires that all of the operator overloading returns a new object.

    There's one little caveat here that's easy to work around.
    With floating point objects (eg. Math::BigFloat and Math::MPFR), values can be altered by changing the precision of the object, but you probably don't have an overloaded operator that you can use to do that.
    Therefore, you need to have a function that provides that capability.
    But that's ok. You just have to make sure that this function returns a new object, rather than simply modifying an existing one.
    (This was the exact issue that led to the Math::MPFR bug report I also mentioned.)

    Cheers,
    Rob

      Thanks for advice. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^7: How does Math::GMP overload the assignment operator ?
by Haarg (Curate) on Nov 10, 2020 at 20:22 UTC

    I didn't propose a solution to "everything". I proposed a solution to the specific problem being discussed.

    You can't accidentally modify a shared object if you can't modify the object at all. I'm not sure what else there is to explain.

      Thanks for advice. I begin to see it clearly now. Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2021-10-17 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?