my $clone = dclone($object);
Storable is a core module, on CPAN you can also find Clone. | [reply] [Watch: Dir/Any] [d/l] [select] |
Clone et al will normally work, but there are some cases where they won't work. Even worse, there are cases where they will partially work in which case you'll get bitten a long way down the line by obscure "can't happen" bugs.
In the general case, you need to use the object's clone() method, and if it doesn't have one - patches welcome!
| [reply] [Watch: Dir/Any] [d/l] |
Especially, decide what you want to do with GLOB objects.
IIRC, Storable will refuse to clone them, but Clone will do it.
| [reply] [Watch: Dir/Any] |
You can do something like this, also, assuming the object is implemented as a hash reference:
my $copy = bless { %$original }, ref $original;
Note that this does not clone any object referenced in the $original (those references will just get copied), so this does the minimal amount of work - while the Storable solution above copies everything it can find.
Quite often, what's actually needed is something in between these two extremes, which means you'll probably end up with the objects providing their own clone() and/or freeze() method(s).
| [reply] [Watch: Dir/Any] [d/l] |