On Thu, 21 Jun 2001, Nick Transier wrote:

> I have written some OO perl and I have a problem that I just realized, when
> you set an object = to another object, they become irreversibly linked and
> all operations on one or the other causes changes in both. If I am trying to
> simply initialize the object and not link them, how do I get around this
> problem?

This is an issue one confronts in a lot of OO langauges.  I think the
issue is that you are not copying objects, but making one reference to
another reference.  You need to create what is called a 'copy constructor'
-- basically, create a method in a class that returns a copy of an object
(I think Randal referred to this the other day when he was talking about
the $self = ref($proto) || $proto controversy), you can call the
constructor 'clone' or something to that effect.  In C++, you can overload
the = operator to use the copy constructor.  I don't remember if in Perl
you can overload =, but you can still do:

$newobj = $old_obj->clone #you will clone $self

or even

$newobj = Class->clone($old_obj);

where Class is the name of the class itself (and clone becomes a class --
or static -- method).

-- Brett
                                   http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
Presidency:  The greased pig in the field game of American politics.
-- Ambrose Bierce

Reply via email to