Hi,

I've read the overload documentation and I understand the basics of overloading the '=' operator.
I also understand that it's not really an overloading of the '=' operator - rather there's a process, that begins with doing $copy = $orig, whereby $copy ultimately becomes a separate copy of the Math::GMP object $orig just prior to taking on a new value.

In Math::GMP this can be demonstrated as:
use strict; use warnings; use Math::GMP; my $orig = Math::GMP->new(2); my $copy = $orig; # At this point $copy and $orig refer to # one and the same Math::GMP object. $copy += 5; # At this point, $copy refers to a newly # created Math::GMP object that holds a # value of 7; $orig still refers to the # original object, holding a value of 2. print "$orig\n"; # prints 2 print "$copy\n"; # prints 7
I have implemented the exact same thing in my Math::GMPz module - just replace all occurrences of "GMP" with "GMPz" and that little demo will behave in exactly the same way.
But now to the bit that I don't understand:

In Math::GMPz that overloading only works because, in GMPz.pm, I've done:
use overload ... , '=' => \&overload_copy, ... ;

If I remove that key-value pair from GMPz.pm then that particular overloading ceases to work. In that demo, both $copy and $orig will then end up referring to the very same (original) object, but with the new value of 7 - and the script crashes on termination.

OTOH, GMP.pm does not supply overload.pm with an '=', sub{} key-value pair at all.
How come this overloading still works for Math::GMP ?

Cheers,
Rob

In reply to How does Math::GMP overload the assignment operator ? 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.