<?xml version="1.0" encoding="windows-1252"?>
<node id="68006" title="How do I make a copy constructor? (inheritance?)" created="2001-03-28 22:57:52" updated="2005-08-13 08:27:47">
<type id="1857">
categorized question</type>
<author id="11732">
QandAEditors</author>
<data>
<field name="doctext">
I'm fairly new to this perl-OO stuff, but I am making headway except for a recent problem.  I've got a package with the follow constructor and accessor methods, but I'm running into problems with creating a copy constructor.

&lt;CODE&gt;
sub new 
{
   my $self;

   $self-&gt;{ A } = undef;
   $self-&gt;{ B } = undef;

   bless $self, 'Stuff';
}

sub A
{
   my ( $self, $a ) = @_;
   $self-&gt;{ A } = $a;
}
&lt;/CODE&gt;
----

I've gotten the copy() method below to work:

&lt;CODE&gt;
sub copy
{
   my ( $self ) = @_;
   my ( $new );

   %{ $new } = %{ $self };

   bless $new, 'Stuff';
   return $new;
}
&lt;/CODE&gt;

The problem that I'm running into is that in the Stuff object I'd like $self-&gt;{ B } to be an array of another type of object (same type of structure, much different data).  Whenever I call the copy constructor I just get a reference to the same array as the initial object, not a true copy.  Any ideas as to what I'm doing wrong, or am I approaching this the wrong way?  
</field>
<field name="parent_node">
1838</field>
</data>
</node>
