I am not aware of any simple command and I rather doubt there is a completely general solution. What can be done depends on your definition of "becomes" and the details of the objects.
Before your "magic" step, $x and $y both refer to one hash and $z refers to another hash.
If you assign $z to $x then $x becomes $z in the sense that $x refers to the same object as $z, but $y will still refer to the original hash blessed into SomeClass.
If you re-bless $x into SomeOtherClass then both $x and $y will both refer to a hash blessed into SomeOtherClass, but its content will not otherwise be modified.
You could then copy the contents of the hash refered to by $z into the hash refered to by $x. This would make $x very similar to $z, but not the same. Depending on the internals of SomeOtherClass, $x and $z may not even refer to distinct instances - they may share data.
You could perform a deep copy from the hash refered to by $z to the hash refered to by $x,but to be completely general you would need a deep copy that can cope with any complexity/magic that exists in the hash. For example, consider what might be necessary if the the hash has an elemnt that referes to a variable that is tied to external (file or database) persistent storage: how should such an element be copied? Consider also that class data should not be replicated. If the hash contains a reference to such class data then the deep copy would have to know not to replicate this. I can't imagine a general purpose algorithm that could deduce such aspects by inspection of the hash.
If SomeOtherClass overloads assignment or has an appropriate copy method then you may achieve what you want by re-blessing $x into SomeOtherClass then assigning or copying $z to $x. In this case, presumably the object will "do the right thing".
In reply to Re: object swizzling?
by ig
in thread object swizzling?
by perl5ever
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |