in reply to How to duplicate objects?

useStorable qw(dclone);
my $clone = dclone($object);

Storable is a core module, on CPAN you can also find Clone.

Replies are listed 'Best First'.
Re^2: How to duplicate objects?
by DrHyde (Prior) on Sep 15, 2008 at 10:05 UTC

    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!

      Especially, decide what you want to do with GLOB objects. IIRC, Storable will refuse to clone them, but Clone will do it.
        I was thinking more of objects which really just store a pointer reference to data in global variables - eg, an object might just be a blessed scalar holding an ID, which is then used internally as the key in a global hash. Such techniques are reasonably common.
Re^2: How to duplicate objects?
by rapide (Beadle) on Sep 15, 2008 at 09:50 UTC
    Thank you!