Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The + overload constructs a new object. Perl uses that overload, and then overwrites the stored value. This means every += op creates a new object

Thanks for the explanation - helps me understand what's going on, and also helps me understand the overload documentation.
Not that it needed to be checked, but with the help of Devel::Peek::Dump() I was able to verify that, with Math::GMP, the address of the object does indeed change with every +=
And I was also able to verify that the same is not true of Math::GMPz.

I find this overloading of '=' to be a bit of a headache, in that it provides a lot more rope than I would like.
WRT Math::GMP, if you have a Math::GMP object ($x) to which you wish to add, say, 4 then there's 2 ways you can do that:
$x += 4; or $x->add_ui_gmp(4);
They both do the same thing in that they increase the value held in $x by 4.
But they don't do the same thing if they were preceded by $x = $orig; where $orig is a Math::GMP object:
$x = $orig; $x += 4; # $orig remains unchanged # $x has been incremented by 4
$x = $orig; $x->add_ui_gmp(4); # Both $x and $orig have # been incremented by 4
AFAIK, the same sort of trap occurs in every math module that overloads '=', including my own Math modules, along with Math::BigInt and Math::BigFloat. (This is not limited to Math::GMP, and I'm not singling it out.)
Although it doesn't really fit with the title I've given this thread, a second question is "Should the documentation of the module (eg Math::GMP) draw attention to this trap ?".
Or is it acceptable to say nothing and put the onus of dodging the trap upon the user ?

BTW, this all started with this Math::MPFR bug report, which is about this same trap in a different module. I keep changing my mind about what, if anything, should be done ....


In reply to Re^2: How does Math::GMP overload the assignment operator ? by syphilis
in thread How does Math::GMP overload the assignment operator ? by syphilis

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2021-10-17 22:54 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (72 votes). Check out past polls.