Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Action at a distance (updated)

by ikegami (Patriarch)
on Nov 03, 2022 at 14:29 UTC ( [id://11147945]=note: print w/replies, xml ) Need Help??


in reply to Re: Action at a distance (updated)
in thread Action at a distance

I wouldn't go with tie (if it even works?). I'd overload = to automatically clone the object on assignment.

Another approach is to use immutable objects. Such a class wouldn't provide a method for incrementing the object; it would provide a method that returns a new object with the higher value.

Both of these approaches add the unnecessary computational and memory overhead of creating clones in situations where they aren't needed.

Replies are listed 'Best First'.
Re^3: Action at a distance
by LanX (Saint) on Nov 03, 2022 at 15:05 UTC
    > I'd overload = to automatically clone the object on assignment.

    Sorry, that's too easy to misunderstand.

    Let me be more precise:

    $b = $a with = overloaded to ->clone will not do an immediate $b =  $a->clone °

    It's rather a kind of copy-on-write.

    After the assignment the refs will still be identical : $b == $a

    The ->clone ("copy" in COW) will only happen delayed just prior to changing $b or $a ("write" in COW)

    But the wording is fuzzy and needs to be tested.

    Anyway this could indeed fix the problem of the OP in an efficient way.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

    °) In Math::BigInt the cloning is done with the method ->copy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found