RiotTown has asked for the wisdom of the Perl Monks concerning the following question: ⭐ (object-oriented programming)
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.
---- I've gotten the copy() method below to work:sub new { my $self; $self->{ A } = undef; $self->{ B } = undef; bless $self, 'Stuff'; } sub A { my ( $self, $a ) = @_; $self->{ A } = $a; }
The problem that I'm running into is that in the Stuff object I'd like $self->{ 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?sub copy { my ( $self ) = @_; my ( $new ); %{ $new } = %{ $self }; bless $new, 'Stuff'; return $new; }
Originally posted as a Categorized Question.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How do I make a copy constructor? (inheritance?)⭐
by japhy (Canon) on Mar 29, 2001 at 04:27 UTC | |
Re: How do I make a copy constructor? (inheritance?)
by BazB (Priest) on Jun 27, 2003 at 21:46 UTC | |
Re: How do I make a copy constructor? (inheritance?)
by Anonymous Monk on Jul 09, 2002 at 08:49 UTC | |
Re: How do I make a copy constructor? (inheritance?)
by Anonymous Monk on Jun 27, 2003 at 18:13 UTC |
Back to
Seekers of Perl Wisdom