Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

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

    No recent polls found