2009/12/28 Christoph Friedrich <christ...@christophfriedrich.de>:
> I have searched the internet but didn't found a good answer.
> How do I clone a Perl Object?
>
> For example like this:
>
> $a = My::Object->new();
> $b = $a->clone();
>
> Where $b is an exact copy of $a but in its own memory.

You call the function that the object provides for this purpose, if any.

The ability to clone an object is not special in Perl; it is treated
just like any other method. Perl doesn't provide a clone method for
you automatically; if you want one in your own objects, you have to
write it yourself. If you want to clone an object from a library
provided by someone else, you have to read its documentation and find
a clone object.

Not all functions provide a clone function; it may not be possible or
desirable to clone a given object. Consider for example an object
which owns a network socket: if you were able to clone the object, you
could create two different objects which own the same socket; this is
a Bad Thing.

Philip

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to