Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

(duplicate; will be deleted) How do I make a copy constructor? (inheritance?)

by RiotTown (Scribe)
on Mar 29, 2001 at 03:43 UTC ( [id://68001]=perlquestion: print w/replies, xml ) Need Help??

RiotTown has asked for the wisdom of the Perl Monks concerning the following question:

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.
sub new { my $self; $self->{ A } = undef; $self->{ B } = undef; bless $self, 'Stuff'; } sub A { my ( $self, $a ) = @_; $self->{ A } = $a; }
---- I've gotten the copy() method below to work:
sub copy { my ( $self ) = @_; my ( $new ); %{ $new } = %{ $self }; bless $new, 'Stuff'; return $new; }
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?

Originally posted as a Categorized Question.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found